firecat318 Posted April 28, 2008 Share Posted April 28, 2008 When people join my website, they choose a username, password, email and a couple other things. I want to have it though, so that later they can fill in some other information to their profile. One of these things is their favorite quote. Here is the profile.php page <? session_start(); $session = $_SESSION['username']; include("connection.php"); ?> <html><head><TITLE>Page</TITLE> <link rel="stylesheet" type="text/css" href="../style.css" /> <meta name="description" content="STUFF" /> </head> <body bgcolor="#E5E5E5"> <? if($session) { $username = mysql_real_escape_string($_GET['user']); $query = "SELECT * FROM `users` WHERE `username` = '$session'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $sql = mysql_query("SELECT `quote` FROM `users` WHERE `username` = '$session'"); $quote = mysql_result($sql, 0); ?> <div id="header"> <table><tr><td align="left"><table> <tr><td width="220px" height="26" id="welcome"> <a href="/"><img src="../images/logo.gif"></a></td></tr> </table> </td><td align="center"> <table> <tr><td align="center"><a href="features.php" id="top_links" class="top">features</a></td><td><b>|</b></td> <td align="center"><a href="forum" id="top_links" class="top">forums</a></td><td><b>|</b></td> <? if($session) { $query = mysql_query("SELECT `username` FROM `users` WHERE `username` = '$session'"); $row = mysql_fetch_assoc($query); ?> <? if($session) { ?> <td align="right"> <a href="logout.php" class="forgot">Logout</a> </td> <? } ?> </table> </div> <br /> <? if($session) { include("header.php"); } ?> <div id="body"> <table width="100%"><tr><td align="left"> <div id="nice" width="200px"> Favorite Quote: <? echo "$quote"; ?> </div> </td></tr></table> </div> (I left some parts out, but that is the main stuff.) Now for the edit.php file, where the user edits his/her profile information. <? session_start(); $session = $_SESSION['username']; include("connection.php"); $query = mysql_query("SELECT `id` FROM `users` WHERE `username` = '$session'"); $id = mysql_result($query, 0); echo "$id"; ?> <b>Edit your profile:</b><br><br><br> <font color=red>Favorite Quote:</font> <form action=" <? $_SERVER['PHP_SELF']; ?> " method="POST"> <textarea cols="40" rows="10" name="favorites"> </textarea><br> <input type="submit" name="quote" value="Update"> </form> <? $sql = mysql_query("INSERT INTO `users` (quote) VALUES('$quote') WHERE `username` = '$session'"); echo "Thank you for updating your profile!"; if(!$sql) { die mysql_error(); } ?> $session is equal to their username, which is logged when they log in. I use this to identify the user when I have to get stuff from the mysql database, but on the edit.php page, when I try to put their quote into their column of information, nothing happens. Link to comment https://forums.phpfreaks.com/topic/103196-help-with-insert-into-mysql/ Share on other sites More sharing options...
dptr1988 Posted April 28, 2008 Share Posted April 28, 2008 You SQL query is incorrect. You need to UPDATE rather then insert Also try escaping the quote before putting it in the query. eg: <?php $escaped_quote = mysql_real_escape_string($quote); $sql = mysql_query("UPDATE `users` SET quote ='{$escaped_quote}' WHERE `username` = '{$session}'"); echo "Thank you for updating your profile!"; if(!$sql) { die mysql_error(); }?> Also are you getting any error messages? Link to comment https://forums.phpfreaks.com/topic/103196-help-with-insert-into-mysql/#findComment-528597 Share on other sites More sharing options...
pocobueno1388 Posted April 28, 2008 Share Posted April 28, 2008 There is no WHERE clause in an INSERT query. Are you sure your not needing to use an UPDATE query? UPDATE users SET quote ='$quote' WHERE username = '$session' Link to comment https://forums.phpfreaks.com/topic/103196-help-with-insert-into-mysql/#findComment-528599 Share on other sites More sharing options...
firecat318 Posted April 28, 2008 Author Share Posted April 28, 2008 Yes, this was my problem. Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/103196-help-with-insert-into-mysql/#findComment-528612 Share on other sites More sharing options...
firecat318 Posted April 28, 2008 Author Share Posted April 28, 2008 Alright, well I fixed the Syntax a little bit, because I got a mysql error, but it still doesn't work. It says it works, but nothing gets inserted into the mysql database. here is my new code <? session_start(); $session = $_SESSION['username']; include("connection.php"); $query = mysql_query("SELECT `id` FROM `users` WHERE `username` = '$session'"); $id = mysql_result($query, 0); echo "$id"; ?> <b>Edit your profile:</b><br><br><br> <font color=red>Favorite Quote:</font> <form action=" <? $_SERVER['PHP_SELF']; ?> " method="POST"> <textarea cols="40" rows="10" name="favorites"> </textarea><br> <input type="submit" name="quote" value="Update"> </form> <? $quote = $_POST['favorites']; $sql = mysql_query("UPDATE `users` SET `quote` = '$quote' WHERE `username` = 'session'"); echo "Thank you for updating your profile!"; if(!$sql) { die(mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/103196-help-with-insert-into-mysql/#findComment-528683 Share on other sites More sharing options...
jonsjava Posted April 28, 2008 Share Posted April 28, 2008 <? session_start(); $session = $_SESSION['username']; include("connection.php"); $query = mysql_query("SELECT `id` FROM `users` WHERE `username` = '$session'"); $id = mysql_result($query, 0); echo "$id"; ?> <b>Edit your profile:</b><br><br><br> <font color=red>Favorite Quote:</font> <form action=" <? $_SERVER['PHP_SELF']; ?> " method="POST"> <textarea cols="40" rows="10" name="favorites"> </textarea><br> <input type="submit" name="quote" value="Update"> </form> <? $quote = $_POST['favorites']; $sql = mysql_query("UPDATE `users` SET `quote` = '$quote' WHERE `username` = '$session'"); echo "Thank you for updating your profile!"; if(!$sql) { die(mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/103196-help-with-insert-into-mysql/#findComment-528688 Share on other sites More sharing options...
firecat318 Posted April 28, 2008 Author Share Posted April 28, 2008 Actually I forgot a little bit of the code lol <? session_start(); $session = $_SESSION['username']; include("connection.php"); $query = mysql_query("SELECT `id` FROM `users` WHERE `username` = '$session'"); $id = mysql_result($query, 0); echo "$id"; ?> <b>Edit your profile:</b><br><br><br> <font color=red>Favorite Quote:</font> <form action=" <? $_SERVER['PHP_SELF']; ?> " method="POST"> <textarea cols="40" rows="10" name="favorites"> </textarea><br> <input type="submit" name="quote" value="Update"> </form> <? $quote = $_POST['favorites']; $sql = mysql_query("UPDATE `users` SET `quote` = '$quote' WHERE `username` = 'session'"); echo "Thank you for updating your profile!"; if(!$sql) { die(mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/103196-help-with-insert-into-mysql/#findComment-528689 Share on other sites More sharing options...
jonsjava Posted April 28, 2008 Share Posted April 28, 2008 $sql = mysql_query("UPDATE `users` SET `quote` = '$quote' WHERE `username` = 'session'"); you are using 'session' instead of '$session' Link to comment https://forums.phpfreaks.com/topic/103196-help-with-insert-into-mysql/#findComment-528692 Share on other sites More sharing options...
firecat318 Posted April 28, 2008 Author Share Posted April 28, 2008 Oops, I think I'm stoned.. I can't even think straight. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/103196-help-with-insert-into-mysql/#findComment-528695 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.