Jump to content

[SOLVED] This code won't insert data into MySQL table...should it?


Chappers

Recommended Posts

Hi,

 

I've got some code which, although I've messed around with it since, was working fine. Now it's not altering the data in the MySQL table. It's not even bringing up the confirmation message to say the data has been entered, nor is any error appearing.

 

<?php

require("connect.php");
$title="Edit Profile";
include("header.php");

$username = $_SESSION['username'];

$result = mysql_query("SELECT status FROM profiles WHERE username='".$username."'") or die 

('Error retrieving status for status check because: ' . mysql_error());

$row = mysql_fetch_assoc($result);
$current_status = $row["status"];
echo "current status is {$current_status}";

if (isset($_POST['submit'])) {
$new_status = $_POST['status'];
mysql_query("UPDATE profiles SET status='$new_status' WHERE username='$username'") or die 

(mysql_error());
echo "<span class='n'>Status $new_status for $username inserted into table</span>";
}

echo "
<form action='{$_SERVER['PHP_SELF']}' method='post'>
<table width='60%' align='center' border='1' cellspacing='0' cellpadding='3'>
<tr>
<td align='center'>
<input type='submit' name='status' value='Single'>
</td>
<td align='center'>
<input type='submit' name='status' value='Married'>
</td>
</tr>
</table>
</form>
";

include('footer.php');

?>

 

Is it my code or might my free host be suffering some kind of glitch?

 

Thanks

Link to comment
Share on other sites

It seems not to be recognising that the submit button has been pressed, as nothing is coming up from an echo placed within the: if (isset($_POST['submit'])) {.

 

Error reporting E_ALL isn't bringing up anything. I just click on the submit button (either Single or Married one) and the page refreshes looking identical, with an echo showing no change to the status field in the MySQL table.

Link to comment
Share on other sites

Hmm, the reason must be because your using

 

<form action="{$_SERVER['PHP_SELF']}" method="post">

 

I've never heard of PHP_SELF... why would you need to use that? Or does it mean keep the same page, but update information? o.O

 

Otherwise, it must be the fact you are trying to update information before the information is set. Hmm, it seemed to work when I moved this:

 

if (isset($_POST['submit'])) {
$new_status = $_POST['username'];
mysql_query("UPDATE Username SET username='$new_status' WHERE username='$username'") or die 

(mysql_error());
echo "<span class='n'>Status $new_status for $username inserted into table</span>";
}

 

Below the form... Try it.

Link to comment
Share on other sites

Hmm, the reason must be because your using

 

<form action="{$_SERVER['PHP_SELF']}" method="post">

 

I've never heard of PHP_SELF... why would you need to use that? Or does it mean keep the same page, but update information? o.O

 

Otherwise, it must be the fact you are trying to update information before the information is set. Hmm, it seemed to work when I moved this:

 

if (isset($_POST['submit'])) {
$new_status = $_POST['username'];
mysql_query("UPDATE Username SET username='$new_status' WHERE username='$username'") or die 

(mysql_error());
echo "<span class='n'>Status $new_status for $username inserted into table</span>";
}

 

Below the form... Try it.

 

None of what you just said would work. They are using $_SERVER['PHP_SELF'] correctly, so that shouldn't be the issue. Also, it is fine that the PHP coding is above the form.

Link to comment
Share on other sites

AndyB, seems I realised the same thing just before coming back here to say I'd found the trouble, only to see you'd already posted it.

 

For some reason, I must have changed the submit button name last night, then forgotten I'd done it tonight. leaving me wondering why it wasn't working. The evils of long work days and staying up till the early hours on the computer. Thanks everyone.

Link to comment
Share on other sites

Ah! I think I have found the problem.

 

This line right here:

if (isset($_POST['submit'])) {

 

You don't have any inputs named "submit", so any code in between isn't even being touched.

 

Try changing that line to this:

 

if (isset($_POST['status'])) {

Link to comment
Share on other sites

Well done, Chappers.  Maybe the occasional beer break would have helped :)

 

If you're happy with this, please use the SOLVED button at the bottom of the page.

 

Ah! I think I have found the problem.

 

Yup, right in this thread :)

Link to comment
Share on other sites

Hmm, the reason must be because your using

 

<form action="{$_SERVER['PHP_SELF']}" method="post">

 

I've never heard of PHP_SELF... why would you need to use that? Or does it mean keep the same page, but update information? o.O

 

Otherwise, it must be the fact you are trying to update information before the information is set. Hmm, it seemed to work when I moved this:

 

if (isset($_POST['submit'])) {
$new_status = $_POST['username'];
mysql_query("UPDATE Username SET username='$new_status' WHERE username='$username'") or die 

(mysql_error());
echo "<span class='n'>Status $new_status for $username inserted into table</span>";
}

 

Below the form... Try it.

 

None of what you just said would work. They are using $_SERVER['PHP_SELF'] correctly, so that shouldn't be the issue. Also, it is fine that the PHP coding is above the form.

...Alright then. I'm quite new to PHP, so, I don't really know way too much... o.O
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.