Jump to content

Random Problem with Variable


LunarIsSexy

Recommended Posts

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;
?>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by LunarIsSexy
Link to comment
Share on other sites

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 by LunarIsSexy
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.