Jump to content

Update MySQL database not working, Help Please


ckerr27

Recommended Posts

Hi everyone, I have been trying to get this code working but no matter what I try it will not work, I am trying to allow a user to update their personal details when they login to my website. I have created a form and submit button, the code is shwn below:

 

<?php

echo $_SESSION['myusername'];

?>

<br><br>

<form action="updated.php" method="post">

Firstname: <input type="text" name="firstname" /><br><br>

Surname : <input type="text" name="surname" /><br><br>

Date Birth: <input type="text" name="dob" /><br><br>

Total Wins: <input type="text" name="wins" />

Total Loses: <input type="text" name="loses" /><br><br>

Email Add: <input type="text" name="email" /><br><br>

Country : <input type="text" name="born" /><br><br>

Other Info: <input type="text" name="other" /><br><br>

<input type="submit" name="Submit" value="Update" align="right"></td>

</form>

 

The updated.php file

 

<?php

mysql_connect ("localhost","root","")

or die("Cannot connect to Database");

mysql_select_db ("test");

 

$sql=mysql_query("UPDATE memberdetails SET firstname='{$_POST['firstname']}', surname='{$_POST['surname']}', dob='{$_POST['dob']}', totalwins='{$_POST['wins']}', totalloses='{$_POST['loses']}', email='{$_POST['email']}', country='{$_POST['born']}', info='{$_POST['other']} WHERE username=$_SESSION['myusername']);

 

if (!mysql_query($sql))

  {

die('Error: ' . mysql_error());

  }

echo "Details Updated";

 

?>

 

This is the error i recieve:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\website\updated.php on line 45

 

Line 45 is $sql=mysql_query line

 

If anybody can help it will be very much appreciated!

 

Link to comment
Share on other sites

$sql=mysql_query("UPDATE memberdetails SET firstname='{$_POST['firstname']}', surname='{$_POST['surname']}', dob='{$_POST['dob']}', totalwins='{$_POST['wins']}', totalloses='{$_POST['loses']}', email='{$_POST['email']}', country='{$_POST['born']}', info='{$_POST['other']} WHERE username=$_SESSION['myusername']");

 

Thanks but it is still giving me the same error.

 

Sorry i am not sure how to use php tags

Link to comment
Share on other sites

I tried this code which is similar to code i used elsewhere but it is still giving me a error on the $sql=mysql_query line:

 

<?php

session_start();

echo $_SESSION['myusername'];

 

mysql_connect ("localhost","root","")

or die("Cannot connect to Database");

mysql_select_db ("test");

 

$sql = "UPDATE  memberdetails WHERE username=$_SESSION['myusername']";

    $result = mysql_query ($sql);

 

      while ($row = mysql_fetch_array($result))

                        {

              $firstname= $row["firstname"];

              $surname= $row["surname"];

              $dob= $row["dob"];

              $wins= $row["totalwins"]; 

              $loses= $row["totalloses"];

              $email= $row["email"];

              $born= $row["country"];

  $other= $row["info"];

 

echo "<b><u>Firstname:</b></u> $firstname<br>";

echo "<b><u>Surname: </b> </u> $surname<br>";

echo "<b><u>Date of Birth:</b></u> $dob<br>";

echo "<b><u>Total Chess Wins:</b></u>  $wins<br>";

echo "<b><u>Total Chess loses:</b></u> $loses<br>";

echo "<b><u>Email Address: </b></u> $email<br>";

echo "<b><u>Born in: </b></u> $born<br>";

echo "<b><u>Other Details:</b></u>  $other<br><br><br>";

          }

?>

 

I am not sure if  am on the correct track or not here?Sorry I am new to PHP

Link to comment
Share on other sites

Yes mate, Sorry I had already realised that updated it,

 

$sql="UPDATE $tbl_name SET firstname='{$_POST['firstname']}', surname='{$_POST['surname']}', dob='{$_POST['dob']}', totalwins='{$_POST['wins']}', totalloses='{$_POST['loses']}', email='{$_POST['email']}', country='{$_POST['born']}', info='{$_POST['other']}' WHERE username='$_SESSION['myusername']'";

   

if (!mysql_query($sql))

  {

die('Error: ' . mysql_error());

  }

  else {

echo "Details Updated";

}

?>

 

Still cannot find what is missing  :'(

 

By the way, I am not after people to debug this I am simply looking help as I am new to PHP and have been trying to get this for days.

 

Thanks everyone who is trying to help

Link to comment
Share on other sites

Strings must be quoted in a MySQL query.

 

You need to learn the basic syntax of the language before dealing with database interactions.

 

If you couldn't find the issues with my guidance, you're probably just blankly staring at characters in a text editor.

 

Start with the basics, work your way up. You'll find solving mis-types and inaccuracies in your code much easier.

 

If you can't debug, you're coding beyond your abilities.

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.