MySQL insert error in PHP -
i've been trying code below execute, although no error appears, row not inserted when check result in database. there seems problem in passing $query $sql, because when echo same value of $query , add text $sql works fine.
any idea error ?
$i=3; $query = '"'."insert data (id,qn,qi,cb,bk,qk)".""."values("."'".$i."','".$price[17]."','".$price[47]."','".$price[77]."','".$price[107]."','".$price[137]."')".'"'; echo $query; $connect= mysql_connect("localhost", "urs" , "password") or die(mysql_error()); mysql_select_db("data", $connect); $sql= $query; mysql_query($sql,$connect)
the problem query has no space right before values
make query syntactically incorrect far mysql goes. render when script runs:
insert data (id,qn,qi,cb,bk,qk)values(…);
mysql choke on query. instead, try version of query 1 space added before values
:
$query = '"'."insert data (id,qn,qi,cb,bk,qk)".""." values("."'".$i."','".$price[17]."','".$price[47]."','".$price[77]."','".$price[107]."','".$price[137]."')".'"';
but said, concatenation makes 100% no sense & unnecessarily complex. i’ve cleaned & should work:
$query = "insert data (id,qn,qi,cb,bk,qk) values('$i','$price[17]','$price[47]','$price[77]','$price[107]','$price[137]');";
the key need understand double quotes in php allow string substation. no need concatenate degree did.
also missing semi-colon after mysql_query()
should this:
mysql_query($sql,$connect);
so here cleaned version of code:
// set value `$1`. $i = 3; // query. $query = "insert data (id,qn,qi,cb,bk,qk) values('$i','$price[17]','$price[47]','$price[77]','$price[107]','$price[137]');"; // set connection or die returning error. $connect = mysql_connect("localhost", "urs" , "password") or die(mysql_error()); // select database; mysql_select_db("data", $connect); // run query or die returning error. mysql_query($query, $connect) or die(mysql_error());
but looking @ selecting database named data
, inserting table within database called data
well? correct? seems might have mixed idea of database & table double check that.
also note added or die(mysql_error());
after mysql_query($query, $connect)
. first or die(mysql_error());
covers errors in connection & not errors appear when query runs. if query has error, die error being returned.
that said, should explore using mysqli_*
commands instead of mysql_*
since mysql_*
commands depreciated in php 5.3 5.4 , eliminated in php 5.5.
Comments
Post a Comment