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);";
// ^ ^ ^ ^
Related
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";
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>";
}
The below PHQL generates a complex resultset like it should:
$phql = "SELECT User.*, ProductUser.* "
. "FROM ProductUser "
. "INNER JOIN User "
. "WHERE ProductUser.product_id = 5";
Replacing ProductUser.* with an existing column like ProductUser.id causes an error:
MESSAGE: The index does not exist in the row
FILE: phalcon/mvc/model/row.zep
LINE: 67
This is version 2.0.6. Is this a bug or am I making a mistake somewhere? According to the documentation it should be fine.
It was my mistake (expecting the row to always be an object).
I hope this helps someone because looping complex resultsets is not in the documentation.
$result = $this->modelsManager->createQuery($phql)->execute();
if ($result instanceof \Phalcon\Mvc\Model\Resultset\Complex) {
foreach ($result as $rows) {
foreach ($rows as $row) {
if (is_object($row)) {
$modelData = $row->toArray());
// this is what I needed
} else {
$id = $row;
}
}
}
}
First of all you're missing the ON clause in your query.
Anyways, it's easier and more error prone to use Phalcon's query builder for querying:
<?php
// modelManager is avaiable in the default DI
$queryBuilder = $this->modelsManager->createBuilder();
$queryBuilder->from(["product" => 'Path\To\ProductUser']);
// Inner join params are: the model class path, the condition for the 'ON' clause, and optionally an alias for the table
$queryBuilder->innerJoin('Path\To\User', "product.user_id = user.id", "user");
$queryBuilder->columns([
"product.*",
"user.*"
]);
$queryBuilder->where("product.id = 5");
// You can use this line to debug what was generated
// $generatedQuery = $queryBuilder->getPhql();
// var_dump($generatedQuery);
// Finish the query building
$query = $queryBuilder->getQuery();
// Execute it and get the results
$result = $query->execute();
// Now use var_dump to see how the result was fetched
var_dump($result);
exit;
I have a select statement see below. Using PDO how would I recreate this same Select statement, as I want to grab two values from it and combine them into the $geomstring. I can figure out the combine, but not the first 3 lines.
$sql1 = "SELECT easting_value, northing_value FROM gridreference_tbl WHERE gridref_id='$_POST[gridref_id]'";
$result1 = pg_query($sql1);
$row1 = pg_fetch_array($result1);
$geomstring = $row1['easting_value']. $_POST['grid_eastings']." ".$row1['northing_value'].$_POST['grid_northings'];
*php website for prepared statements says *
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
I have something similar working for populating a dropdown that partly uses this
$stmt = $conn->prepare("SELECT easting_value, northing_value FROM gridreference_tbl WHERE gridref_id=$gridref_id");
$stmt->setFetchMode(PDO::FETCH_OBJ);
Found it on php.net, I was googling the wrong stuff:
$stmt4 = $conn->prepare("SELECT easting_value, northing_value from gridreference_tbl WHERE gridref_id = 4");
$stmt4->execute();
print("PDO::FETCH_ASSOC: ");
print("Return next row as an array indexed by column name\n");
$result = $stmt4->fetch(PDO::FETCH_ASSOC);
print_r($result);
print("\n");
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";