akano Posted January 21, 2009 Share Posted January 21, 2009 I've got a little problem with my query. This is my database: TABLE: Members Columns: id username password name age email I already got a working form that fills in id,username and password but when I now have constructed a script to fill in the rest of the columns I can't get it working. I my INSERT statement to put the rest of the data together with a specified user. I thought it would look something like this: INSERT INTO members (name,age,email) VALUES ('test',23,'test') where username='username'; or: INSERT INTO members (name,age,email) WHERE username='username' VALUES ('test',23,'test'); Anyone know what I'm doing wrong? ??? Quote Link to comment https://forums.phpfreaks.com/topic/141796-beginners-bad-luck/ Share on other sites More sharing options...
Mchl Posted January 21, 2009 Share Posted January 21, 2009 When the row already exists in database, you need to use UPDATE http://dev.mysql.com/doc/refman/5.0/en/update.html UPDATE members SET name = 'test', age = 23, email = 'test' WHERE username = 'username' Quote Link to comment https://forums.phpfreaks.com/topic/141796-beginners-bad-luck/#findComment-742354 Share on other sites More sharing options...
akano Posted January 21, 2009 Author Share Posted January 21, 2009 Thanks a lot Mchl it worked like a charm I just need help with one more thing and I don't feel like taking up space in another subforum although this question is more a php problem. I try to use this query: mysql_query("UPDATE members SET name='$name', age=$age, email='$email', WHERE username='akano'")or die(mysql_error()); but it doesn't work but works well in sql..is there something I've missed? Thanks for the fast help btw! Quote Link to comment https://forums.phpfreaks.com/topic/141796-beginners-bad-luck/#findComment-742422 Share on other sites More sharing options...
Mchl Posted January 21, 2009 Share Posted January 21, 2009 Remove the comma (,) before WHERE Quote Link to comment https://forums.phpfreaks.com/topic/141796-beginners-bad-luck/#findComment-742424 Share on other sites More sharing options...
Maq Posted January 21, 2009 Share Posted January 21, 2009 email='$email', Not sure if this is the problem but, you don't need this comma before the WHERE clause. Do you get any errors or it just doesn't update? Quote Link to comment https://forums.phpfreaks.com/topic/141796-beginners-bad-luck/#findComment-742425 Share on other sites More sharing options...
akano Posted January 21, 2009 Author Share Posted January 21, 2009 well I removed the (,) but it still doesn't work. I tried to remove the variables and insert some values instead. It still won't update. And no I don't get any errors, the page just comes up blank and when I check the database nothing has happend. In the command prompt the UPDATE command works perfectly but not in the script. Kinda wierd :/ Quote Link to comment https://forums.phpfreaks.com/topic/141796-beginners-bad-luck/#findComment-742430 Share on other sites More sharing options...
Maq Posted January 21, 2009 Share Posted January 21, 2009 Then you have an error somewhere before you try to execute the sql statement. Can we see your code? Also put this at the top of your page: ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/141796-beginners-bad-luck/#findComment-742433 Share on other sites More sharing options...
akano Posted January 21, 2009 Author Share Posted January 21, 2009 Yeah sure here is the form: <html> <body> <form action="mataindata.php" method="post"> <table border ="0"> <tr><td>Your Name:</td><td> <input type="text" name="name" maxlength="40"> </td></tr> <tr><td>E-mail:</td><td> <input type="text" name="email" maxlength="40"> </td></tr> <tr><td>Age:</td><td> <input type="text" name="age" maxlength="40"> </td></tr> <tr><td colspan="2" align="right"> <input type="submit" value="Submit"> </td></tr> </table> </form> <a href=lol.php>Hem</a> </body> </html> And here is the php code: <? ini_set ("display_errors", "1"); error_reporting(E_ALL); $name=$_POST['name']; $email=$_POST['email']; $age=$_POST['age']; mysql_connect("localhost", "root", "password") or die(mysql_error()); mysql_select_db("projekt") or die(mysql_error()); mysql_query("UPDATE members SET name='$name', age=$age, email='$email' WHERE username='akano'")or die(mysql_error()); echo "Your information has been successfully added to the database."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/141796-beginners-bad-luck/#findComment-742442 Share on other sites More sharing options...
fenway Posted January 27, 2009 Share Posted January 27, 2009 Looks fine, but echo the query. Quote Link to comment https://forums.phpfreaks.com/topic/141796-beginners-bad-luck/#findComment-747499 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.