LunarIsSexy Posted September 20, 2013 Share Posted September 20, 2013 So this is my code and for some reason nothing displays on the screen at all. It should echo $page or $latestpage and that should be a link it saved in the database but it didn't save anything to the database and it didn't echo anything. Any clue? <?php include 'connect.php'; include 'main.php'; $id=$_SESSION['id']; $page = "http://localhost/page1.php"; $sql="UPDATE math SET latestpage=$page where id='$id'"; mysql_query($sql); $result3 = mysql_query("SELECT * FROM html where id='$id'"); while($row3 = mysql_fetch_array($result3)) { $latestpage=$row3['latestpage']; echo $latestpage; ?> Quote Link to comment https://forums.phpfreaks.com/topic/282330-random-problem-with-variable/ Share on other sites More sharing options...
DFulg Posted September 21, 2013 Share Posted September 21, 2013 Where is your debugging code? Quote Link to comment https://forums.phpfreaks.com/topic/282330-random-problem-with-variable/#findComment-1450504 Share on other sites More sharing options...
LunarIsSexy Posted September 21, 2013 Author Share Posted September 21, 2013 On 9/21/2013 at 12:21 AM, DFulg said: Where is your debugging code? I'm not very great with PHP but this would be the correct function to call a debug on a variable right? <?php function debug_view ( $page ) { echo '<pre>'; if ( is_array( $page ) ) { print_r ( $page ); } else { var_dump ( $page ); } echo '</pre>'; } ?> At the moment I don't have any debug code. Quote Link to comment https://forums.phpfreaks.com/topic/282330-random-problem-with-variable/#findComment-1450505 Share on other sites More sharing options...
DFulg Posted September 21, 2013 Share Posted September 21, 2013 On 9/21/2013 at 12:25 AM, LunarIsSexy said: I'm not very great with PHP but this would be the correct function to call a debug on a variable right? <?php function debug_view ( $page ) { echo '<pre>'; if ( is_array( $page ) ) { print_r ( $page ); } else { var_dump ( $page ); } echo '</pre>'; } ?> At the moment I don't have any debug code. No. include 'connect.php'; include 'main.php'; $id = $_SESSION['id']; $page = "http://localhost/page1.php"; $sql = "UPDATE math SET latestpage=$page WHERE id='$id'"; mysql_query($sql) or die("MYSQL Query Failed : " . mysql_error()); $result3 = mysql_query("SELECT * FROM html WHERE id='$id'") or die("MYSQL Query Failed : " . mysql_error()); while($row3 = mysql_fetch_array($result3)) { $latestpage=$row3['latestpage']; echo $latestpage; Note: mysql is deprecated and will be removed in a future version of PHP. Use mysqli or PDO instead. Quote Link to comment https://forums.phpfreaks.com/topic/282330-random-problem-with-variable/#findComment-1450506 Share on other sites More sharing options...
LunarIsSexy Posted September 21, 2013 Author Share Posted September 21, 2013 (edited) On 9/21/2013 at 12:31 AM, DFulg said: No. include 'connect.php'; include 'main.php'; $id = $_SESSION['id']; $page = "http://localhost/page1.php"; $sql = "UPDATE math SET latestpage=$page WHERE id='$id'"; mysql_query($sql) or die("MYSQL Query Failed : " . mysql_error()); $result3 = mysql_query("SELECT * FROM html WHERE id='$id'") or die("MYSQL Query Failed : " . mysql_error()); while($row3 = mysql_fetch_array($result3)) { $latestpage=$row3['latestpage']; echo $latestpage; Note: mysql is deprecated and will be removed in a future version of PHP. Use mysqli or PDO instead. Thankyou for that! It now debugs saying this. MYSQL Query Failed : 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 '://localhost/page1.php WHERE id='8'' at line 1 I think this is due to the fact that there are no values or anything data saved under html, so it can't find anything with the ID 8. Any idea what to do next? I guess instead of updating its going to have to just add a new one and if there is already something with that ID it will just update it. Any idea? Edited September 21, 2013 by LunarIsSexy Quote Link to comment https://forums.phpfreaks.com/topic/282330-random-problem-with-variable/#findComment-1450508 Share on other sites More sharing options...
PaulRyan Posted September 21, 2013 Share Posted September 21, 2013 On the first query, you need to surround the $page variable with quotes. Quote Link to comment https://forums.phpfreaks.com/topic/282330-random-problem-with-variable/#findComment-1450511 Share on other sites More sharing options...
LunarIsSexy Posted September 21, 2013 Author Share Posted September 21, 2013 (edited) On 9/21/2013 at 12:48 AM, PaulRyan said: On the first query, you need to surround the $page variable with quotes. That got rid of the error but this code still doesn't work. This is what I have 100% and nothing happens. Blank page and nothing shows up in database. <?php include 'connect.php'; include 'main.php'; $id = $_SESSION['id']; $page = "http://localhost/page1.php"; $sql = "UPDATE html SET id='$id', lastpage='$page' WHERE id='$id'"; mysql_query($sql) or die("MYSQL Query Failed : " . mysql_error()); $result3 = mysql_query("SELECT * FROM html WHERE id='$id'") or die("MYSQL Query Failed : " . mysql_error()); while($row3 = mysql_fetch_array($result3)) { $lastpage=$row3['lastpage']; echo $lastpage; } ?> Edited September 21, 2013 by LunarIsSexy Quote Link to comment https://forums.phpfreaks.com/topic/282330-random-problem-with-variable/#findComment-1450512 Share on other sites More sharing options...
PaulRyan Posted September 21, 2013 Share Posted September 21, 2013 Echo out both queries to make sure they contain exactly what you expect. Then check the database for the id you are updating/selecting. Quote Link to comment https://forums.phpfreaks.com/topic/282330-random-problem-with-variable/#findComment-1450513 Share on other sites More sharing options...
LunarIsSexy Posted September 21, 2013 Author Share Posted September 21, 2013 I made it so when you register it will just set your ID inside the table and set the lastpage as page1.php. This is a very weird way to do it but it works out! Thanks for all your guys help! Quote Link to comment https://forums.phpfreaks.com/topic/282330-random-problem-with-variable/#findComment-1450514 Share on other sites More sharing options...
LunarIsSexy Posted September 21, 2013 Author Share Posted September 21, 2013 On 9/21/2013 at 1:18 AM, PaulRyan said: Echo out both queries to make sure they contain exactly what you expect. Then check the database for the id you are updating/selecting. The data wouldn't update in the database at all because there was no default data for that ID. When I updated it I was updating it based on whatever column had the ID field matching the session ID. Idk how else to do it so I just set a default by clicking insert in PHPMyAdmin. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/282330-random-problem-with-variable/#findComment-1450515 Share on other sites More sharing options...
mac_gyver Posted September 21, 2013 Share Posted September 21, 2013 there's an INSERT ... ON DUPLICATE KEY UPDATE query that will do what you want - http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html Quote Link to comment https://forums.phpfreaks.com/topic/282330-random-problem-with-variable/#findComment-1450541 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.