user2060451
user2060451

Reputation: 2688

HTML form to update Mysql with PHP (and HTML)

I've been trying to develop a real estate page where people can add listings. I am new to the world of php mysql. I have been over this problem for over a day and can't figure out where the problem is. I have a form where people can add data. That's good and working. Now I am starting to have a place where people can add / delete / update their info. I am trying to build this step by step.

This is where a user could pull the information. My problem is with the piece of the code:

edit_form.php?idBAR=$row[id]. 

Full code below.

<table>
  <tr>
    <td align="center">EDIT DATA</td>
  </tr>
  <tr>
    <td>
      <table border="1">
      <?php
      include"configS_OH.php";//database connection
      $order = "SELECT * FROM braaasil_brokerstour.property";
      $result = mysql_query($order);
      while ($row=mysql_fetch_array($result)){
          echo ("<tr><td>$row[id]</td>");
        echo ("<td>$row[address]</td>");
        echo ("<td>$row[day]</td>");
        echo ("<td>$row[hours]</td>");
        echo ("<td><a href=\"edit_form.php?idBAR=$row[id]\">Edit</a></td></tr>");
      }
      ?>
      </table>
    </td>
   </tr>
</table>

Then this tutorial try to pass id through the address bar (I don't know much about php to actually say much)

It tries to upload the data into a new form where a person could edit info. But I can't load the data into the new form. If I use where id=7, I get the info into the form. But this method of passing the info in the address bar like ?idBAR=8... and then try to catch it in the other code (where id=$idBAR), is not working. Here is the code:

<table border=1>
  <tr>
    <td align=center>Form Edit Employees Data</td>
  </tr>
  <tr>
    <td>
      <table>
      <?php
      include "configS_OH.php";//database connection
      print $database;
      $order = "SELECT * FROM braaasil_brokerstour.property 
WHERE id='$idBAR'";
print $idBAR;
      $result = mysql_query($order)  or die( mysql_error() );
      $row = mysql_fetch_array($result);

      ?>
      <form method="post" action="edit_data.php">
      <input type="hidden" name="idBAR" value="<?php echo "$row[id]"?>">
        <tr>        
          <td>Address</td>
          <td>
            <input type="text" name="address" 
        size="20" value="<?php echo "$row[address]"?>">
          </td>
        </tr>
        <tr>
          <td>Date</td>
          <td>
            <input type="text" name="day" size="40" 
          value="<?php echo "$row[day]"?>">
          </td>
        </tr>
        <tr>
          <td>Time</td>
          <td>
            <input type="text" name="time" size="40" 
          value="<?php echo "$row[hours]"?>">
          </td>
        </tr>
        <tr>
          <td align="right">
            <input type="submit" 
          name="submit value" value="Edit">
          </td>
        </tr>
      </form>
      </table>
    </td>
  </tr>
</table>

I tried an tried and tried.. Thank you for your time in advance.

Upvotes: 1

Views: 705

Answers (4)

BBrown
BBrown

Reputation: 38

The problem is the $row[id] is seen as text just like everything else. You want the value of $row[id]. Instead of

echo ("<td><a href=\"edit_form.php?idBAR=$row[id]\">Edit</a></td></tr>");

try

echo ("<td><a href=\"edit_form.php?idBAR=" . $row[id] . "\">Edit</a></td></tr>");

Upvotes: 0

chiliNUT
chiliNUT

Reputation: 19573

if you are putting a variable directly in a string without concatenating, it can't be an array variable; you must concatenate those you also need to surr. so this

echo ("<td><a href=\"edit_form.php?idBAR=$row[id]\">Edit</a></td></tr>");

should be this

echo ("<td><a href=\"edit_form.php?idBAR=".$row['id']."\">Edit</a></td></tr>");

also, it looks like your form is sending data with POST. When you pass form data in the url string after the question mark, that is passing with get.

so...in your form where you want to use that variable, you set it up like this

$idBAR=$_GET['idBAR']; //to get the variable if it was part of the URL

$idBAR=$_POST['idBAR']; //if it was sent with post, as is the case with your form

also, request contains both get and post, so

$idBAR=$_REQUEST['idBAR'];

will work in either case.

Upvotes: 1

James
James

Reputation: 4783

I don't see anywhere you have actually used the GET data, just the reference name which is used in GET.

If you first query is working and is getting the $row['id'] value ok - you can verify this when you go to edit_form.php, in your browser URL bar at the top, does it say this:

edit_form.php?idBAR=7

(or whatever number should be there)

If so, then you just need to use the PHP GET. Your data is stored in $_GET[], and in this case, the reference name is idBAR. So your id from your previous page query is sent through the link into your URL, and on your edit_form.php page, you'd use that data as:

$_GET['idBAR']

You can use that, but personally I assign the data to a variable, such as:

$strGetId = $_GET['idBAR'];

Then you can use $strGetId throughout your code. Also, check things like isset(), empty() etc, just so you know you are working with A) something is actually there, and B) it's not empty etc

Upvotes: 1

Andy G
Andy G

Reputation: 19367

WHERE id='$idBAR'

You haven't assigned $idBAR any value. You need to read it from the $_GET array first:

$idBAR = $_GET['idBAR'];

You should, of course, check that this value exists first, and is acceptable.

Upvotes: 1

Related Questions