edit the dynamic data from repeat region in php - edit

I want to edit a record set based on FileNo, which is linked with another table with fileno as the linking fields. I am here providing the two set of codes, one is working and other is giving the code error.
Working code:
$sql="SELECT * FROM casedt WHERE AdvUser= '".$_SESSION['MM_Username']."' && FileNo = $fileno ";
$result= mysqli_query($link, $sql) or die ('Unable to run query:'.mysql_error());
#$count=mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
echo "<table border='1'><tr><th>Adv User</th><th>File No.</th><th>Next Dt</th><th>Comments</th><th></th></tr>";
// output data of each row
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>".$row["AdvUser"]."</td><td>".$row["FileNo"]."</td><td>".$row["NextDt"]."</td><td>".$row["Comments"]."</td></tr>";
}
Non-working code:
$sql="SELECT * FROM casedt WHERE AdvUser= '".$_SESSION['MM_Username']."' && FileNo = $fileno ";
$result= mysqli_query($link, $sql) or die ('Unable to run query:'.mysql_error());
#$count=mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
echo "<table border='1'><tr><th>Adv User</th><th>File No.</th><th>Next Dt</th><th>Comments</th><th></th></tr>";
// output data of each row
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>".$row["AdvUser"]."</td><td>".$row["FileNo"]."</td><td>".$row["NextDt"]."</td><td>".$row["Comments"]."</td><td><a href=EditDate.php?recordID= .$row['FileNo'] Edit</a></td</td></tr>";
}

Related

mysqli inserts typed values but not variables

I have tried to get the following code to work. It is an ajax-call where I send a json-encoded string 'data':
<?php
require_once('connect.php'); // get values for mysqli-connect
$arr1 = json_decode($_GET['data']);
$arr = array_values($arr1);
if ($GLOBALS['TSFE']->loginUser) {
$mysqli = new mysqli($server,$user,$pw,$db);
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
//values to be inserted in database table
$var1 = "Hello World";
$var2 = 45;
$var3 = "an encoded array";
/*
$var1 = $arr[1];
$var2 = $arr[2];
$var3 = json_encode($arr[3]);
*/
$insert_row = $mysqli->query("INSERT INTO table_testing
(event, pid, myarray) VALUES ('$var1', '$var2', '$var3')");
if($insert_row){
print 'Success! ID of last inserted record is : ' .$mysqli->insert_id .'<br />';
}else{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
} else {
echo "User is NOT logged in!\n";
}
?>
My problem is that this code works! It works fine with the three values that are stored in $var1-3. They are saved in the correct table.
But - if I try to store the out-commented three values from $arr[1-3] into $var[1-3] it doesn't work? The three values saved are ' ' (empty), '0' and 'NULL'.
I have tried to put these lines in right after $arr is declared:
echo $arr[1]."\n";
echo $arr[2]."\n";
echo json_encode($arr[3])."\n";
In the console the correct values are printed, so they are valid and exist. It is a string, an integer and an array.
What is it I have overlooked or not understood?
Indexes of PHP arrays start are 0-based.
Try with this code:
echo $arr[0] . "\n";
echo $arr[1] . "\n";
echo json_encode($arr[2]) . "\n";

PDO Exception 42000

I have tried to insert data into my database but I keep getting this error that I can't figure out. Been all over the forums and cant find an answer so I thought I would post.
Full error for reference.
error
Code:
<?php
$Records = $connect->query('SELECT COUNT(ID) as counted FROM listings;');
$fetchdata = $Records->fetch();
$i = $fetchdata['counted'];
$i++;
$name = 'hold';
$OS = 0;
if(isset($_POST['name'])){
$name = $_POST['name'];
$type = $_POST['type'];
$loc = $_POST['loc'];
$desc = $_POST['desc'];
$prem = $_POST['premium'];
}
if($name != 'hold'){
$sql = "INSERT INTO listings (name,premium,location,type,desc,onsite,ID,paymentid) VALUES (:Name,:Prem,:Loc,:Type,:Desc,:Onsite,:ID,:Paymentid);";
$Appquery = $connect->prepare($sql);
$Appquery->bindParam(':Name', $name, \PDO::PARAM_STR);
$Appquery->bindParam(':Prem', $prem, \PDO::PARAM_INT);
$Appquery->bindParam(':Loc', $loc, \PDO::PARAM_STR);
$Appquery->bindParam(':Type', $type, \PDO::PARAM_STR);
$Appquery->bindParam(':Desc', $desc, \PDO::PARAM_STR);
$Appquery->bindParam(':Onsite', $OS, \PDO::PARAM_INT);
$Appquery->bindParam(':ID', $i, \PDO::PARAM_INT);
$Appquery->bindParam(':Paymentid', $i, \PDO::PARAM_INT);
$Appquery->execute();
header("Location: index.php");
}
?>
Both desc and type are reserved MySQL keywords. Just quote them as:
$sql = "INSERT INTO listings (name,premium,location,`type`,`desc`,onsite,ID,paymentid) VALUES (:Name,:Prem,:Loc,:Type,:Desc,:Onsite,:ID,:Paymentid);";
// ^ ^ ^ ^

How can I use sql data in header link?

I need to use a variable from SQL. I can print which I wrote the variable. But I can't use
it when it out of while query.
Here is the SQL:
if ($result = $mysqli->query("SELECT * FROM organize WHERE organize.nid=$nid"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
while ($row = $result->fetch_object())
{
echo $row->nid;
echo $row->omid;
$id=$row->omid;
}
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
And the header under this scripts.
header("Refresh: 10;http://localhost/records.php?mid= $id ");
I know this is not right way. But I show you what I want. Need to use that 'omid' in header inseat of '$id'.
Finally I resolved it. This scripts like the above codes. I didn't understand why It didn't work. Maybe because I should not use
echo $row->nid;
echo $row->omid;
or
$id=$row->omid;
The right usage is as follows:
while ($row = $result->fetch_object())
{
$dd=$row->omid;
}
header("Refresh: 2;http://localhost/records.php?mid= $dd");

PDO retrieve all fields with value 1 and echo with alternate name

i have a table (facility) in which field names are swimming_pool, restaurant, WiFi, library etc.
if a particular house having only two of those facilities available, i give those available field names value 1 and if not 0.
I want to retrieve all the field names with value 1 and echo each field with value 1 with an alternate name.
here is what i want to achieve
$st = $db->prepare(select * from facility where house_id = ?);
$st->execute(array($_GET['house_id']));
$row = $st->fetch();
from above i have to select all fields with value 1. And for all those fields with value one i have to echo name (for example: if swimming_pool value is 1 i have to echo Swimming Pool, if it is 0 then check for the next column)
i hope you understood my goal.
I hope I solved your question? I didn't test it yet, so if there might be a problem, do a comment.
<?php
/*
* Sample Link: web.com/index.php?house_id=1&search=1,0,0,1
*/
/*
* Sample Fields:
* swimming_pool, restaurant, wifi, library
*/
//Explode the $_GET['search'] to create an array
$fields_to_search = explode(",",$_GET['search']);
//Manually set the fields
$db_fields = [
'swimming_pool',
'restaurant',
'wifi',
'library'
];
$fields_to_be_selected = "";
//Create a counter as basis to store if the Link is 1 or 0, then pass it to $db_fields[]
$counter = 0;
foreach($fields_to_search as $field) {
//Check if $field is 1
if($field == 1) {
//Then pass it to $table_to_be_selected and add a COMMA
$fields_to_be_selected .= $db_fields[$counter].",";
}
$counter++;
}
//Remove the last COMMA
$fields_to_be_selected = substr($fields_to_be_selected,0,-1);
/*
* LASTLY DO THE prepare()
*/
$query = sprintf("SELECT %s FROM `facility` WHERE `house_id` = ?",$fields_to_be_selected);
$stmt = $db->prepare($query);
$stmt->execute(array($_GET['house_id']));
$rows = $stmt->fetchAll();
//It will only print the selected fields
print_r($rows);
?>

perl, dbi with sql statement with a like condition

in my code i must do a simple sql query with a like condition.
i've do in this way
my $out = "/Users/zero/out.log";
my $filename = "/Users/zero/data.txt";
my $dbh = DBI->connect("DBI:Oracle:sid=$sid;host=$host;port=$port", $user, $pwd) or die "Couldn't connect to database: " . DBI->errstr;
my $query = "select SOMETHING from SOMETHING_1 where SOMETHING like ?||'%' ";
my $sth = $dbh->prepare($query) or die "Connection Error: " . $dbh->errstr;
open (IN,"< $filename") or die("Unable to open $filename");
my #righe = <IN>;
close IN;
open (OUT,">$out") or die "Unable to open $out";
foreach my $riga (#righe) {
chomp $riga;
(my $valore) = split (/\n/, $riga);
$sth->execute($valore) ||print "Impossibile eseguire la query $query";
while (my $real = $sth->fetchrow_array) {
print OUT "\"$real\"\n";
}
}
$sth->finish;
$dbh->disconnect();
but the query return all the rows, ignoring the like condition.
Where's my fault?
Thank's
You have to concat the % char to the variable you search for.
my $query = "select SOMETHING from SOMETHING_1 where SOMETHING like ?";
...
$sth->execute($valore.'%') ||print "Impossibile eseguire la query $query";