Jump to content

cannot store record in the database.


jimfog

Recommended Posts

I cannot store a record in the database. Here is the insert statement-I want to make sure the syntax is correct. I want to focus on this first and thenlook for other pieces of code:

$result = $conn->query("insert into users values						 (NULL, '".$username."', sha1('".$passwd."),'".$name."','".$phone."')");  if (!$result) {    throw new Exception('Could not register you in database - please try again later.');

As you understand I get "Could not register you in database - please try again later". Is there any way I can get more info about the cause of the error?

Link to comment
Share on other sites

Put this at the end of your query:

or die(mysql_error());

for the info you need.

Link to comment
Share on other sites

Put this at the end of your query:
or die(mysql_error());

for the info you need.

You mean inside the parentheses, where the variables are listed?
Link to comment
Share on other sites

Just put it at the end of your query:

query("insert into users values (NULL, '".$username."', sha1('".$passwd."),'".$name."','".$phone."')") or die(mysql_error());

Link to comment
Share on other sites

which library are you using? is it pdo? you have to use appropiate error handling function according to your api. http://au.php.net/manual/en/pdo.errorinfo.php this is for pdo

Link to comment
Share on other sites

which library are you using? is it pdo? you have to use appropiate error handling function according to your api. http://au.php.net/manual/en/pdo.errorinfo.php this is for pdo
I do not use a library and it is the first time I hear the term PDO
Just put it at the end of your query:
 query("insert into users values (NULL, '".$username."', sha1('".$passwd."),'".$name."','".$phone."')") or die(mysql_error()); 

I used the code you gave me-with the die statement-in the end and the only thing I get is a blank page, not any message at all. Here is the code:
$result = $conn->query("insert into users values (NULL, '".$username."', sha1('".$passwd."),'".$name."','".$phone."')")		  or die(mysql_error());

In the beginning I assumed that the connection was succesful and the db had been updated with the new record, but that is not the case. The db has not been updated and the strange is that I do not get any error message telling me this or that. Any ideas?

Link to comment
Share on other sites

I wouldn't assume anythingthing when I comes to working an error. In fact, the first question a repair man asks is "do you have your machine plugged in". If all you get is a blank screen, either something's not plugged in, or your query executed, but isn't working as expected. It doesn't appear your insert query is targeting any columns http://www.w3schools.../sql_insert.asp EDIT: Can your system execute a simple SELECT?

Edited by niche
Link to comment
Share on other sites

I do not use a library and it is the first time I hear the term PDO
then from where does the $conn->query() is coming? if it is any custom method what does it returns? it is always best to store the query in variable so that you can print it for debug like jsg said. Edited by birbal
Link to comment
Share on other sites

then from where does the $conn->query() is coming? if it is any custom method what does it returns? it is always best to store the query in variable so that you can print it for debug like jsg said.
Look at this code to understand the origin of conn-> query:
function db_connect() {   $result = new mysqli('localhost', 'root', '19471979', 'appointments');   if (!$result) {	 throw new Exception('Error.');   } else {	 return $result;   }}$conn = db_connect();

$conn = db_connect() is part of another function NOT the one you see above.

Link to comment
Share on other sites

I managed to output an error which I am having difficult to explain. First here is the code the "fires" the initiation of errors:

$result = $conn->query("insert into users values						 (NULL, '".$username."', sha1('".$passwd."),'".$name."','".$phone."')") or die(mysqli_error($conn));

And here is the 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 'kolovos','6973999098')' at line 2 

Kolovos and the number you see next to it are the data($name,$phone) I want to insert to the table of the db-these are filled in a form first.I cannot understand what syntax error is there.

Link to comment
Share on other sites

Guest So Called

I sometimes find the solution to my query problems by echoing the query (to get the actual values used) then cut 'n paste that into phpMyAdmin to run the query manually, which can sometimes give a more obvious error message.

Link to comment
Share on other sites

you missed the quotesha1('".$passwd."),'".$nawill be

sha1('".$passwd."'),".$na

concatenation creates lots of missunderstanding. it is better to avoid that where possible. you can easily usevariable inside double quotes also can use interpolation when dealing with array.

Link to comment
Share on other sites

Yahoooo, it works, thanks, so much effort finally payed of. Yes you are right string concatenation sometimes is a headache

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...