I am having a hard time figuring out the exact code. It is possible I have the wrong syntax or a very limited knowledge of doing the same. Any assistance is appreciated.
$Game = array('ASIN' => $field->ASIN,
'title' => $field->title,
'price' => $field->price,
'quantity' => $field->quantity);
$sql = "INSERT INTO GameTable (`ASIN`, `Title`, `Price`, `Quantity`) "
. "VALUES ($Game['ASIN'], $GAME['title'], "
. "$GAME['price'], $GAME['quantity'])"
this code:
$sql = "INSERT INTO GameTable (`ASIN`, `Title`, `Price`, `Quantity`) "
. "VALUES ($Game['ASIN'], $GAME['title'], "
. "$GAME['price'], $GAME['quantity'])"
should become
$asin = $game['asin'];
$title = $game['title'];
$price = $game['price'];
$quantity = $game['quantity'];
$sql = "INSERT INTO GameTable (`ASIN`, `Title`, `Price`, `Quantity`)
VALUES ('$asin', '$title', '$price', '$quantity')";
Take care of using capital letters noting that php variables are case sensitive.
Related
I am trying to insert a row into my data base, but what ever i seem to do I am always getting an error.
Sometimes I get parse errors and sometimes I get column errors.
Here is my code.
Thanks in advance.
<?php
include_once('config.php');
$asin = $_POST['asin'];
// $title = "<script>document.write(title)</script>";
// $mpn = "<script>document.write(mpn)</script>";
// $price = "<script>document.write(price)</script>";
$sql = "INSERT INTO `amazon`.`amazon` (`asin`, `title`, `mpn`, `price`) VALUES ($asin, "test", 1, 2)";
// $sql = 'INSERT INTO amazon'.
// '(asin, title, mpn,price) '.
// 'VALUES ('{$asin},' "test", 1, 2)';
mysql_select_db('amazon');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
?>
You need to use '' against your variable $asin as:
$sql = "INSERT INTO `amazon`.`amazon` (`asin`, `title`, `mpn`, `price`) VALUES ('".$asin."', 'test', 1, 2)";
Note:-
You can't use double quotes in double quotes. Replace double
quote(") with single quote(') around test value.
use variables in single quotes.(Example - $asin to '$asin')
Replace your query with this:-
$sql = "INSERT INTO `amazon`.`amazon` (`asin`, `title`, `mpn`, `price`) VALUES ('$asin', 'test', 1, 2)";
You have made two mistakes. in
$sql = "INSERT INTO `amazon`.`amazon` (`asin`, `title`, `mpn`, `price`) VALUES ($asin, "test", 1, 2)";
$asin and "test".
if $asin is an integer value always THEN it's okay otherwise you have to write it '".$asin."'
and for "test" the error is the comma you use here (") because you query is starting with same (") comma, so when you put same comma before test then query ends here and give you error. So replace this comma by (').
replace "test" by 'test'.
Now correct query is -
$sql = "INSERT INTO `amazon`.`amazon` (`asin`, `title`, `mpn`, `price`) VALUES ('".$asin."', 'test', 1, 2)";
where is error.....
Column count doesn't match value count at row 1
mysqli_select_db($conn, $data);
$save = "INSERT INTO SONG_AS(ALBUM, CATEGORY, SUB_CATEGORY,
SONG_NAME, ARTIST, ART_LINK,
YEAR, GENRE, SONG_LINK, POST_ON,
POST_BY, TOTAL_DOWNLOAD)
VALUES('$albm', '$cat', '$scat', "
. "'$sn', '$art', '$img', "
. "'$y', '$g', "
. "'$sl', '$time', '', '0')";
$success = mysqli_query($conn, $save) or die(mysqli_error($conn));
$page = "index.php";
$this->pageRedirect($page);
where is error.....
Column count doesn't match value count at row 1
remove '' from your query to be:
$save = "INSERT INTO SONG_AS(ALBUM, CATEGORY, SUB_CATEGORY,
SONG_NAME, ARTIST, ART_LINK,
YEAR, GENRE, SONG_LINK, POST_ON,
POST_BY, TOTAL_DOWNLOAD)
VALUES('$albm', '$cat', '$scat','$sn', '$art', '$img', '$y', '$g', '$sl', '$time', '', '0')";
Try this:
$save = "INSERT INTO SONG_AS(`ALBUM`, `CATEGORY`, `SUB_CATEGORY`,
`SONG_NAME`, `ARTIST`, `ART_LINK`,
`YEAR`, `GENRE`, `SONG_LINK`, `POST_ON`,
`POST_BY`, `TOTAL_DOWNLOAD`)
VALUES('".$albm."', '".$cat."', '".$scat."','".$sn."', '".$art."', '".$img."', '".$y."', '".$g."', '".$sl."', '".$time."', '', '0')";
$query = "INSERT INTO employee VALUES ($empno','$lname','$fname','$init','$gender','$bdate','$dept','$position','$pay','$dayswork','$otrate','$othrs','$allow','$advancesance,'')";
$msg = "New record saved!";
}
else {
$query = "UPDATE employee SET empno=$empno','lname='$lname',fname='$fname',init= '$init',gender='$gender',bdate='$bdate',dept='$dept',position='$position',pay=$pay,dayswork=$dayswork,otrate=$otrate,othrs=$othrs,allow=$allow,advances=$advances,insurance=$insurance WHERE empno = $empno";
$msg = "Record updated!";
}
I am working on this application that is supposed to collect data from a form and send it to the database but for some reason, the database is not receiving any data. I tried using mysql_errno() and I received "Query was empty"
$name = mysql_real_escape_string($_POST['name']);
$qualification = mysql_real_escape_string($_POST['qualification']);
$position = mysql_real_escape_string($_POST['position']);
$direct = mysql_real_escape_string($_POST['direct']);
$telephone = mysql_real_escape_string($_POST['telephone']);
$cell = mysql_real_escape_string($_POST['cell']);
$fax = mysql_real_escape_string($_POST['fax']);
$email = $r['email'];
$address = mysql_real_escape_string($_POST['address']);
$cityProvince = mysql_real_escape_string($_POST['cityProvince']);
$postalCode = mysql_real_escape_string($_POST['postalCode']);
$website = mysql_real_escape_string($_POST['website']);
$q = mysql_query("INSERT INTO `mmg_business_card_logs` (`id`, `name`, `qualification`, `position`, `direct`, `telephone`, `cell`, `fax`, `email`, `address`, `cityProvince`, `postalCode`, `website`)
VALUES ('', '{$name}', '{$qualification}', '{$direct}', '{$position}', '{$telephone}', '{$cell}', '{$fax}', '{$email}', '{$address}', '{$cityProvince}', '{$postalCode}', '{$website}')");
$q = mysql_query("INSERT INTO `mmg_business_card_logs` ( `name`, `qualification`, `position`, `direct`, `telephone`, `cell`, `fax`, `email`, `address`, `cityProvince`, `postalCode`, `website`)
VALUES ('$name', '$qualification', '$direct', '$position', '$telephone', '$cell', '$fax', '$email', '$address', '$cityProvince', '$postalCode', '$website')");
OR
$q = mysql_query("INSERT INTO `mmg_business_card_logs` (`id`, `name`, `qualification`, `position`, `direct`, `telephone`, `cell`, `fax`, `email`, `address`, `cityProvince`, `postalCode`, `website`)
VALUES ('121', '$name', '$qualification', '$direct', '$position', '$telephone', '$cell', '$fax', '$email', '$address', '$cityProvince', '$postalCode', '$website')");
Try this . If this will not work try echo $q and show me the output or try to execute that query in MySql
I am taking table name as php variable to insert data into table.
But it gives error. What's bad here?
if($flag == 1)
$table = 'frrole_pupolar_article';
else
$table = 'frrole_category_article';
$insertQuery1 = "INSERT INTO '.$table.' (`url`, `sentiment`, `category`, `title` ,`time`,`img_url`,`rt_count`,`tweet_count`) VALUES ('".$url."','".$setiment."','".$category."','".$title."','".$time."','".$img_url."','".$rt_count."','".$tweet_count."')";
error:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''.frrole_category_article.' (`url`, `sentiment`, `category`, `title` ,`time`,`im' at line 1
$insertQuery1 = "INSERT INTO '" .$table. "' (`url`, `sentiment`, `category`, `title` ,`time`,`img_url`,`rt_count`,`tweet_count`) VALUES ('".$url."','".$setiment."','".$category."','".$title."','".$time."','".$img_url."','".$rt_count."','".$tweet_count."')";
You wrongly written '.$table.' instead of '" .$table. "'
Just do
$insertQuery1 = "INSERT INTO $table (`url`, `sentiment`, `category`,
`title` ,`time`,`img_url`,`rt_count`,`tweet_count`)
VALUES ('".$url."','".$setiment."','".$category."',
'".$title."','".$time."','".$img_url."','".$rt_count."',
'".$tweet_count."')";
remove the concatenations and single quotes
If your string is $insertQuery1 = "INSERT INTO '.$table.' (url,sentiment,
You cannot escape it by using single quotes '..'
Just do double quotes:
$insertQuery1 = "INSERT INTO ".$table." (`url`, `sentiment`, ....
also for all that is holy, use damn {} for if(){} statements, not using them is poor form
This is my code.
// insert reward into wallet
$sql = "
INSERT INTO `wallet` (`uid`, `created_at`, `amount`, `type`, `payment_id`) VALUES (:uid, CURRENT_TIMESTAMP, :amount, 'payment', :payment_id);
";
$sth = self::link()->prepare($sql);
// primary key makes sure payment does not get double rewarded
$sth->execute(
array(
':uid' => $referer,
':amount' => $reward,
':payment_id' => $payment_data['payment_id'],
)
);
var_dump(self::link()->errorInfo());
self::log("issuing subscription",self::LOG_LEVEL_DEBUG);
// extend referers subscription
$tid = self::link()->lastInsertId();
var_dump(self::link()->errorInfo());
self::log("using $tid as id for wallet transfer",self::LOG_LEVEL_DEBUG);
My log says:
[2011-07-02 20:31:44] using 0 as id for wallet transfer
However the insert query is successful, the database record is created and both errorInfo outputs give no error.
Remove the semicolon or the whitespace after the semicolon or both from your query:
$sql = "
INSERT INTO `wallet` (`uid`, `created_at`, `amount`, `type`, `payment_id`) VALUES (:uid, CURRENT_TIMESTAMP, :amount, 'payment', :payment_id)
";