Jump to content

Recommended Posts

Hey guys,

Below is some code which I thought I had working....

 

<?php
$result = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_assoc($result)){

if ($row['booked'] == 'yes') {
echo "<td bgcolor=lightblue><b>Booked</b><br>$row[name]<br><form action=edit.php method=post><input type=hidden name=date value=$row[date]><input type=submit value=View/Edit></td>";
}
elseif ($row['booked'] == 'no') {
echo "<td bgcolor=lightgray>Empty<br><form action=edit.php method=post><input type=hidden name=date value=$row[date]><input type=submit value=Add></td>";
}
}
?>

This populates a table with either 'Booked' with their name, or 'Empty' ... depending on the data in the database.

The button is meant to post the date to the following page, and this seemed to work at first, but now the following page is getting seemingly random dates.

 

Here's what it looks like...

<?php
$date = $_POST['date'];
mysql_connect("######", "########", "#########") or die(mysql_error());
mysql_select_db("########") or die(mysql_error());
$query = "SELECT * FROM ####### WHERE date = '" . $date . "'";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){

echo "<form name=###### method=post action=update.php>";
echo "<tr bgcolor=white><td>";
echo "Property:<br><input type=text name=property value=Chateau readonly><p>";
        echo "</td><td>";
        echo "Date:<br><input type=text name=date value=$date readonly>";
        echo "</td><td>";
...

As I said, it seemed to post the date from the correct row at first, but now I'm getting seemingly random dates... and therefore the following 'update.php' edits the wrong row in the database!!

For the life of me I cannot figure out why...

 

Some help from you PHP freaks would be great :D

Link to comment
https://forums.phpfreaks.com/topic/120985-solved-this-is-so-confusing/
Share on other sites

Try this

 

<?php
$result = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_assoc($result)){

if ($row['booked'] == 'yes') {
echo '<td bgcolor=lightblue><b>Booked</b><br>'.$row['name'].'<br><form action="edit.php" method="post"><input type="hidden" name="date" value="'.$row['date'].'"><input type="submit" value="View/Edit"></td>';
}
elseif ($row['booked'] == 'no') {
echo '<td bgcolor="lightgray">Empty<br><form action="edit.php" method="post"><input type="hidden" name="date" value="'.$row['date'].'"><input type="submit" value="Add"></td>';
}
}
?>

Nope haha,

Using '. .' seems to be invalid.

It didn't find any data in my database haha...

 

I think its a problem with it finding the wrong row, since its getting the date from a different row in my database.

Eg. when I specify that the hidden input's value is "value=$row[date]", it gets the date from a different row, and not the one its currently on!

 

Oh dear...

 

???

Do a "show source" on the generated HTML to see what the script is generating. You may have spaces in some of the variables which will screw up your form, since you don't quote any of the value attributes in your HTML tags.

 

Ken

Hi Ken,

I did a show source, the html looks good, in the first cell it says the hidden value is "0501" which is what it should be.

This makes me think there's something wrong with my edit.php to make it appear as "2712" all the time.

<?php
$date = $_POST['date'];
mysql_connect("######", "########", "#########") or die(mysql_error());
mysql_select_db("########") or die(mysql_error());
$query = "SELECT * FROM ####### WHERE date = '" . $date . "'";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){

echo "<form name=###### method=post action=update.php>";
echo "<tr bgcolor=white><td>";
echo "Property:<br><input type=text name=property value=Chateau readonly><p>";
        echo "</td><td>";
        echo "Date:<br><input type=text name=date value=$date readonly>";
        echo "</td><td>";
...

 

Hmmm....

Oh I think I get it!

 

It's getting the POST from the last one executed!

Which is why it seemed random earlier, since the page hadn't fully loaded it got the last one to be executed haha...

 

I guess I'll need to do this a different way, any suggestions?

with this:

<?php

if ($row['booked'] == 'yes') {
echo '<td bgcolor=lightblue><b>Booked</b><br>'.$row['name'].'<br><form action="edit.php" method="post"><input type="hidden" name="date" value="'.$row['date'].'"><input type="submit" value="View/Edit"></td>';
}
elseif ($row['booked'] == 'no') {
echo '<td bgcolor="lightgray">Empty<br><form action="edit.php" method="post"><input type="hidden" name="date" value="'.$row['date'].'"><input type="submit" value="Add"></td>';
}

 

If the only 2 values for booked are yes or no, you dont need the elseif, just put a else  ;)

<?php
else {

You're going to have to start putting in debug statements to dump values. Looking at the code you posted doesn't help.

 

Put

<?php
echo '<pre>' . print_r($_POST,true) . '</pre>';
?>

at the top of the processing script to dump what is being sent from the form.

 

Print out your query, etc...

 

Ken

Right,

MasterAce, I copied and pasted that exactly, but exactly the same porrrblem!

 

Ken,

 

Array

(

    [date] => 2712

)

 

It should be posting 0501, not 2712, which is the last date in the loop.

 

I also tried giving the form a unique name (I used the date), I thought that would give the form a different name each time it looped through it and thus solving the problem but no :(

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.