This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
I'm not sure why it's not inserting any data.
No errors are returned.
I'm new in the mysql scene so i might be doing something wrong..
Do you guys mind pointing me towards the right direction?
$link = mysqli_connect("localhost", "root", "", "testdatabase");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
else if ($command == 'create-key'){
$keys = $_GET['nkey'];
if (empty($_GET['nkey'])){
print('Error: No key specified to create!');
die();
}
print ('Key '. $_GET['nkey'] .' has been created.');
$sql = ("INSERT INTO `keys` (`key`, `status`) VALUES ('. $keys .', 0)");
}
SQL Code:
CREATE TABLE `keys` (
`key` varchar(15) NOT NULL,
`status` int(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
There is no need of ()
Change Query From
$sql = ("INSERT INTO `keys` (`key`, `status`) VALUES ('. $keys .', 0)");
To
$sql = "INSERT INTO `keys` (`key`, `status`) VALUES ('$keys', 0)";
And then Execute this Query
You'll probably need this on top of your script to see PHP errors:
<?php
ini_set('display_errors', 'on');
error_reporting ( E_ALL );
To help you with your error, have a look at your query.
$sql = ("INSERT INTO `keys` (`key`, `status`) VALUES ('. $keys .', 0)");
While this is should insert . $keys . in your table, please try:
$sql = ("INSERT INTO `keys` (`key`, `status`) VALUES ('". $keys ."', 0)");
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)";
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 6 years ago.
My PHP Code:
<?php
//Connecting to sql db.
$mysqli = new mysqli("127.0.0.1", "admin", "pass", "enedpt_faculties");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
mysqli_set_charset($mysqli,"utf8");
$user = $_POST['user'];
$pass = $_POST['pass'];
$nome = $_POST['nome'];
$apelido = $_POST['apelido'];
$fac = $_POST['fac'];
$data = $_POST['data'];
$email = $_POST['email'];
mysqli_query($mysqli,"`enedpt_faculties`.`users` (`user`, `pass`, `name`, `sname`, `facid`, `nasc`, `mail`)
VALUES ('$user', '$pass', '$nome', '$apelido, '$fac', '$data', '$email')");
?>
For some reason the query is not inserting, and there are no errors on the error Log. Please Help.
You have to uae correct SQL.
An insert statement looks like this:
'INSERT INTO "table" (columnnames) VALUES (values)';
In your specific case you also have to concatenate the variables to the SQL-string:
'INSERT INTO `users` (`user`, `pass`, `name`, `sname`, `facid`, `nasc`, `mail`) VALUES ('.$user.', '.$pass.', '.$nome.', '.$apelido.', '.$fac.', '.$data.', '.$email.')'
You also can use back tics on the variables. Also you had some mistakes like missing quotes.
What is the proper way to reference the array components from fetch_assoc() so that they can be inserted into another table?
Here is my current code:
$sql_read = "SELECT id, data1, data2, date FROM `table1`";
$result = $mysqli->query($sql_read);
if ($result !== false) {
$rows = $result->fetch_all();
}
while ($row = $result->fetch_assoc()){
$sql_write = "INSERT INTO `table2`.`load_records` (`id`, `data1`,`data2`,`date`) VALUES ('.$row['id']', '.$row['data1']', '.$row['data2']', '.$row['date']', NULL);";
}
As suggested in my comment, first your select statement should be:
"SELECT id, data1, data2, date FROM `table1`";
Furthermore, the insert statement should be (see the use of concatenation of strings):
"INSERT INTO `table2`.`load_records` (`id`, `data1`,`data2`,`date`) VALUES ('".$row['id']."', '".$row['data1']."', '".$row['data2']."', '".$row['date']."', NULL);";
There are some errors in your script, as already pointed out.
But you might also be interested in the INSERT INTO ... SELECT variant of the INSERT syntax.
<?php
$query = '
INSERT INTO
table2
(`id`, `data1`,`data2`,`date`)
SELECT
`id`, `data1`,`data2`,`date`
FROM
table1
';
$result = $mysqli->query($query);
if ( !$result ) {
trigger_error('error: ' . $mysqli->error, E_USER_ERROR);
}
else {
echo $mysqli->affected_rows, ' rows have been transfered';
}
You have an extra field in the VALUES that is not referenced in the INTO and the concatenation of the row data is incorrect;
$sql_write = "INSERT INTO `table2`.`load_records`
(`id`, `data1`,`data2`,`date`)
VALUES ('.$row['id'].', '.$row['data1']', '.$row['data2']', '.$row['date']', NULL);";
should be:
$sql_write = "INSERT INTO `table2`.`load_records`
(`id`, `data1`,`data2`,`date`)
VALUES ('".$row['id']."', '".$row['data1']."', '".$row['data2']."', '".$row['date']."');";
Or you need to update the INTO to include an extra column that accepts NULL
See also:
The PHP reference on mysqli_result::fetch_assoc
<?
include("../../panel/inc/config.php");
$ip = $_SERVER['REMOTE_ADDR'];
// Insert the log
$insert = "INSERT INTO logs (log, ip, date) VALUES ('{$log}', '{$ip}', '{$date}')";
mysql_query($insert) or die("MySQL Error - Could not insert reviews");
$date = date("d/m/y - h:ia");
$insertLog = "INSERT INTO `logs` ( `log` , `ip`, `date` ) VALUES ('viewed test page', '$date')";
mysql_query($insertLog) or die('MySQL Error - Could not insert a log.');
?>
basically when someone views this page, I want it to insert into the database, but it's not inserting. I get the Error for inserting log.
Any ideas?
My database is
$insertLog = "INSERT INTO `logs` ( `log` , `ip`, `date` ) VALUES ('viewed test page', '$date')";
missing one column value here. You have three columns but two values in above query. It seems that you have missed ip value in above query.
you should try like this:
$ip = $_SERVER['REMOTE_ADDR'];
if(isset($ip)){
// Insert the log
$insert = "INSERT INTO logs (log, ip, date) VALUES ('{$log}', '{$ip}', '{$date}')";
mysql_query($insert) or die('MySQL Error - ' . mysql_error() );
$date = date("d/m/y - h:ia");
$insertLog = "INSERT INTO `logs` ( `log` , `ip`, `date` ) VALUES ('viewed test page','$ip' '$date')";
mysql_query($insertLog) or die('MySQL Error - ' . mysql_error() );
}
notice: all mysql_* functions are deprecated. You should move to PDO or mysqli.
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