1

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.

4 Answers 4

1
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.

Sign up to request clarification or add additional context in comments.

2 Comments

Also note that the mysql extension is deprecated and you should use prepared statements (parameterized queries).
Thank you! it worked! Interesting that 3 other answers were the same... but I got it working here. Yes, I need to work with mysqli. As a person learning by himself, PDO doesn't make much sense as a code for a human being to read... so I will update corrent code to mysqli. I just need to get it working first to understand the mechanism of pulling data, working with php inside the html etc. Thank you so much.
1

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

1 Comment

This is correct. It worked as suggested down next answer. Thank you very much for your time. $idBAR = $_GET['idBAR'] before the sql line.
1

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.

5 Comments

Using $_REQUEST is not advisable and the array-element can be embedded in the string if surrounded by curly brackets - concatenation is not essential.
ah true for the brackets. $_REQUEST just put out there to show what his options are.
please bear with me. I am new to this and what might be simple for some... so in one part of the code we have the link edit that shoots a url with the id number where the update is suppose to take place. The second form (where I am trying to upload the info) has a standard sql select * ..... where id=$idBAR (trying to catch which id to upload). I guess I can't use that after the WHERE, can I?
THIS is CORRECT, at least the part where it says $idBAR=$_GET['idBAR']. It just happened that I got it to work with the other answer, which is exactly the same. Put answered there, upvoted here. Thank you very much for your time.
happy PHPing. Its a fun language, I hope you enjoy using it.
0

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>");

1 Comment

This is what I get in the address bar: openhouse.braaasil.com/edit_form.php?idBAR=8 (number 8 random pick - could be any number). Still does not pass value to new table. I hadn't tried that before. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.