zeek69 Posted June 7, 2008 Share Posted June 7, 2008 I have created a database with 2 tables, one is called test with 5 fields and I have a php script that adds data from a form to the database without error. I have a second table with 7 fields with basically the same php script to add data to it, but it won't enter the data. Not sure what is wrong, the tables seem to be setup identically, and are in the same database. Here are the 2 scripts. I figure it is something stupid that is causing this problem, but I have been staring at this for days with no luck. Any help would be greatly appreciated. Zeek <? $DBhost = "xxxxxxxx.ipowermysql.com"; $DBuser = "xxxx"; $DBpass = "xxxx"; $DBName = "xxxxx"; $table = "test"; mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database"); @mysql_select_db("$DBName") or die("Unable to select database $DBName"); $name = mysql_real_escape_string($_POST['name']); $email = mysql_real_escape_string($_POST['email']); $age = mysql_real_escape_string($_POST['age']); $country = mysql_real_escape_string($_POST['country']); $sqlquery = "INSERT INTO $table VALUES('$id','$name','$email','$age','$country')"; $results = mysql_query($sqlquery); mysql_close(); print "<html><body><center>"; print "<p>You have just entered this record<p>"; print "Name : $name br"; print "Email : email br"; print "Age : $age br"; print "Country : $country br"; print "</body></html>"; ?> ----------------------------------------------------------------------------------------------- <? $DBhost = "xxxxxxxx.ipowermysql.com"; $DBuser = "xxxx"; $DBpass = "xxxx"; $DBName = "xxxx"; $table = "items"; mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database"); @mysql_select_db("$DBName") or die("Unable to select database $DBName"); $item = mysql_real_escape_string($_POST['item']); $high = mysql_real_escape_string($_POST['high']); $low = mysql_real_escape_string($_POST['low']); $paid = mysql_real_escape_string($_POST['paid']); $qty = mysql_real_escape_string($_POST['qty']); $date = mysql_real_escape_string($_POST['date']); $sqlquery = "INSERT INTO $table VALUES('$id','$item','$high','$low','$paid','$qty','$date')"; $results = mysql_query($sqlquery); mysql_close(); print "<html><body><center>"; print "<p>You have just entered this record<p>"; print "Item : $item br"; print "High : $high br"; print "Low : $low br"; print "Paid : $paid br"; print "Qty : $qty br"; print "Date : $date br"; print "</body></html>"; ?> Please use code tags when posting code ~ CV Quote Link to comment Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 add ... or die(mysql_error()); to the end of your mysql function calls see if it give you a message. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 7, 2008 Share Posted June 7, 2008 Pleasssssssssssssse add "or die(mysql_error())" after your query. I don't understand why people can't do that. Quote Link to comment Share on other sites More sharing options...
zeek69 Posted June 7, 2008 Author Share Posted June 7, 2008 Crayon, Thanks, with that addition I found out that it was giving me a duplicate entry error. The ID field wasn't set to auto increment. Once I go that resolved, it worked fine. I new it was something simple. Zeek Quote Link to comment Share on other sites More sharing options...
zeek69 Posted June 7, 2008 Author Share Posted June 7, 2008 DarkWater, Newbie to PHP. Sorry. Zeek Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 7, 2008 Share Posted June 7, 2008 Just for future reference...ALWAYS add that line after your queries. ALWAYS. It makes debugging so much simpler. Edit: Sorry for being so angry before, lol, but there are at least 5 threads a day that are solved by having them add that line. It's really annoying after a while. =P Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted June 7, 2008 Share Posted June 7, 2008 I disagree. That's a crap way of doing error handling. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 7, 2008 Share Posted June 7, 2008 I disagree. That's a crap way of doing error handling. It makes debugging queries a hell of a lot simpler, especially because it's a MySQL error and not a PHP error, so it won't be sent through any error handlers. Unless you throw an exception for failed MySQL queries, which I think is overkill and it's only in PHP5+, so therefore he might not be able to do it. Please explain why it's a crap way of doing error handling. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted June 7, 2008 Share Posted June 7, 2008 Well, what are you going to do when you go live with the application/site? I sure don't hope you're going to leave the or die() after the function call. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 7, 2008 Share Posted June 7, 2008 Obviously not. That reveals too much info. For production sites I use custom error handlers and such, but for debugging, it makes much more sense to handle the error directly. Quote Link to comment Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 haha yeah I just suggested it to help him find his bug, that's all. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted June 7, 2008 Share Posted June 7, 2008 Obviously not. That reveals too much info. For production sites I use custom error handlers and such, but for debugging, it makes much more sense to handle the error directly. Why not just write the error handler on before hand and use that? The PHP5 argument you used above doesn't hold. PHP has officially stopped supporting PHP4 so there is no reason whatsoever not to upgrade. Furthermore, why do you think exceptions would be overkill? haha yeah I just suggested it to help him find his bug, that's all. Sure, and it did, but he would've been better off designing his application properly from the start. Quote Link to comment Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 Sure, and it did, but he would've been better off designing his application properly from the start. Yeah...tell that to 99% of the people who come here who have no formal education in php or even programming in general. The bottom line is that they don't care, nor will they ever care. They want a quick and dirty solution, so I give it as much as possible. This isn't some hospital or dr's office where people strive to go by the book 100% of the time. This is a warzone and we're field medics pulling McGuyver-like operations to keep the soldiers up. Suck it up soldier! Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 7, 2008 Share Posted June 7, 2008 Obviously not. That reveals too much info. For production sites I use custom error handlers and such, but for debugging, it makes much more sense to handle the error directly. Why not just write the error handler on before hand and use that? The PHP5 argument you used above doesn't hold. PHP has officially stopped supporting PHP4 so there is no reason whatsoever not to upgrade. Furthermore, why do you think exceptions would be overkill? haha yeah I just suggested it to help him find his bug, that's all. Sure, and it did, but he would've been better off designing his application properly from the start. 1) Some webhosts don't have PHP 5 yet, sadly. Kind of strange, but it's true. Also, like CV said, lots of people on these forums have never even HEARD of exceptions. 2) If he had designed it properly from the start, he wouldn't be posting here. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted June 7, 2008 Share Posted June 7, 2008 Sure, and it did, but he would've been better off designing his application properly from the start. Yeah...tell that to 99% of the people who come here who have no formal education in php or even programming in general. The bottom line is that they don't care, nor will they ever care. They want a quick and dirty solution, so I give it as much as possible. This isn't some hospital or dr's office where people strive to go by the book 100% of the time. This is a warzone and we're field medics pulling McGuyver-like operations to keep the soldiers up. Suck it up soldier! That's a shame, because I could name a few people (but I won't, at least not publicly) that I would hate to take over a project from. Also, if I were to buy anything I don't just want it to work, I want it to work properly, but the problem is that I'm not competent enough to say if it's good or bad. Obviously not. That reveals too much info. For production sites I use custom error handlers and such, but for debugging, it makes much more sense to handle the error directly. Why not just write the error handler on before hand and use that? The PHP5 argument you used above doesn't hold. PHP has officially stopped supporting PHP4 so there is no reason whatsoever not to upgrade. Furthermore, why do you think exceptions would be overkill? haha yeah I just suggested it to help him find his bug, that's all. Sure, and it did, but he would've been better off designing his application properly from the start. 1) Some webhosts don't have PHP 5 yet, sadly. Kind of strange, but it's true. Also, like CV said, lots of people on these forums have never even HEARD of exceptions. 2) If he had designed it properly from the start, he wouldn't be posting here. At 1: Switch host. They're not worth bothering with. Regarding the exceptions. IMO people should read the syntax chapter of the manual before even trying to create any code. At 2: True, but that's the reason why we should teach them the correct ways instead of just continuing bad practice just because the rest of the application is written so. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.