Jump to content

Issue writing in MySQL Database using php


Kamicase

Recommended Posts

Hello everyone, after getting some code on and my WAMP server revved up I am now trying to write the input from a form into a MySQL Database.The script I am using runs like this:

	 <?php		  $alpha=""; $beta=""; $gamma=""; $theta="";		  if (isset($_POST['alpha'])) $alpha = $_POST['alpha'];		  if (isset($_POST['beta'])) $beta = $_POST['beta'];		  if (isset($_POST['gamma'])) $gamma = $_POST['gamma'];		  if (isset($_POST['theta'])) $theta = $_POST['theta']; 		mysql_connect("localhost","root"); // I'm running the Database local and for learning purposes.		mysql_select_db("database"); 			$write = "INSERT INTO table (name, lastname, color, comment)				VALUES ('$alpha', '$beta', '$gamma', '$theta')";			mysql_query($write) or die ('Error writing in database.');	?>

When I run the script though (having entered some dummy information in all 4 fields)I had x OR DIE checks for both mysql_connect and mysql_select_db and they both confirmed that everything went fine so the bug must crawl through mysql_query($write) but I just can't seem to pin it down.Does anyone know where I might be wrong?

  • Like 1
Link to comment
Share on other sites

What is $alpha=""; $beta=""; $gamma=""; $theta=""; used for and you could try

if (empty($var)) {	echo '$var is either 0, empty, or not set at all';}

instead of if isset uou can do this below if you want

if (isset($_POST['Submit'])) { }

And what is your form action & method.

Edited by Mudsaf
Link to comment
Share on other sites

When a query isn't working, print out its value to see if it has what you expected:

echo $write;

On a sidenote, the empty() function also considers the string "0" to be empty, which is why I don't like using it.

Link to comment
Share on other sites

Guest So Called
... the bug must crawl through mysql_query($write) but I just can't seem to pin it down.Does anyone know where I might be wrong?
What bug? What error?
Link to comment
Share on other sites

@Mudsaf: Not sure anymore... I used to get some errors without it but I took it out to check which ones and it works just fine now. So I'll leave it without. The form looks like this:

<form method="post" action="GreekSubmission.php">

@So Called: Ah... Did I forget to actually say what went wrong? ^^ Well the mysql_query($write) OR DIE ('Error updating database') ends with the process dying and the error message being displayed. So the final step, the updating of the database, fails. @Ingolme: Very good idea. I did that and it gave me: "INSERT INTO table (name, lastname, color, comment) VALUES ('Dummyvalue', 'Testtext', 'Purple', 'I hope this works') so nothing out of place it seems. Is there something wrong with my syntax?

Edited by Kamicase
Link to comment
Share on other sites

Guest So Called

So do like Ing said and echo $write to see what the query string looks like. Then if the error isn't obvious, cut 'n paste that into your database manager (phpMyAdmin?) query window and see what you get. Also try this query via manual entry:

INSERT INTO table (name, lastname, color, comment) VALUES ('something', 'blah', 'foo', 'bar')

Also try a query in PHP code with valid values stuffed into the query instead of variables. Did you define your table structure? I'm assuming the fields are text.

Link to comment
Share on other sites

@So Called: I did echo $write. That was in my response to Ings post but it didn't show anything weird. But thank you for reminding me to try the query window. There it has shown me, that I miswrote 'comment' where it should have been 'comments'. It's working great now. Thanks to all of you. : )

Link to comment
Share on other sites

Guest So Called

Okay, I misread your post but I'm glad you got it all working. Bugs are often like this. You write the code and then you check it but your eyes see what you think you typed, not what is actually there.

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...