Jump to content

Load SQL Variable & Edit Right Away...


noiselab

Recommended Posts

Hi everyone, just thought I'd register at a PHP forum, I'm a graphic designer that's just now starting to learn a little of PHP and SQL and I've had a few little things I'm not sure of how to fix, so I thought I'd turn here for some help.

 

I am making a simple test database that's hosted on my hosting account and I've got one php page that pulls out all the values from a DB table and sorts them into input fields that are sorted inside a table. Now the reason I insert those values into a input field is because I want to make another form function where those loaded values can be changed and saved right away. My code for right now is this :

 

<body>
<p align="center"><span class="style1"><br />
    <span class="style2">SQL Readout Test : </span></span><br />
    <br />
    <br />
</p>
<form action="testphp2.php" method="post" >
  <div align="center">
    <table width="500" border="0" cellspacing="0" cellpadding="0">
      <tr><th></th>
      <th>Input Fields : </th>
      <th>Comments and Complaints : </th>
      </tr><tr>
        <td width="111" align="left" valign="top"><strong>Firstname : </strong></td>
        <td width="156" align="left" valign="top"><input name="firstname" type="text" size="30" /></td>
        <td width="329" rowspan="5" align="left" valign="top"><div align="right">
          <textarea name="comment" cols="50" rows="10" id="comment"></textarea>          
          <label>
          <div align="right"><br />
            <input name="submit" type="submit" value="Submit Info" />
          </div>
        </label></td>
      </tr>
      <tr>
        <td align="left" valign="top"><strong>Lastname : </strong></td>
        <td align="left" valign="top"><input name="lastname" type="text" size="30" /></td>
      </tr>
      <tr>
        <td align="left" valign="top"><strong>Age
: </strong></td>
        <td align="left" valign="top"><input name="age" type="text" id="age" size="30" /></td>
      </tr>
      <tr>
        <td align="left" valign="top"><strong>E-Mail
: </strong></td>
        <td align="left" valign="top"><input name="email" type="text" id="email" size="30" /></td>
      </tr>
      <tr>
        <td height="27" align="left" valign="top"><strong>Location 
: </strong></td>
        <td align="left" valign="top"><input name="location" type="text" id="location" size="30" /></td>
      </tr>
    </table>
    <br />
    <br />
  </div>
</form>
<?php
$con = mysql_connect("hostname","db_user","db_password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("db_name", $con);

$result = mysql_query("SELECT * FROM test_userinfo");

echo "<table align='center' width='500px' border='0'>
<tr>
<th>Firstname :</th>
<th>Lastname :</th>
<th>Age :</th>
<th>Email :</th>
<th>Location :</th>
<th>Comments</th>
</tr>";while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><input size=\"10\" name=\"firstname\" type=\"text\" id=\"firstname\" value=\"$row[firstName]\" /></td>";
  echo "<td><input size=\"10\" name=\"lastname\" type=\"text\" id=\"lastname\" value=\"$row[lastName]\" /></td>";
    echo "<td><input size=\"4\" name=\"age\" type=\"text\" id=\"age\" value=\"$row[userAge]\" /></td>";
  echo "<td><input size=\"25\" name=\"email\" type=\"text\" id=\"email\" value=\"$row[userEmail]\" /></td>";
    echo "<td><input size=\"25\" name=\"location\" type=\"text\" id=\"location\" value=\"$row[userLocation]\" /></td>";
	  echo "<td><input  size=\"60\" name=\"comments\" type=\"text\" id=\"comments\" value=\"$row[userComments]\" /></td>";
  echo "</tr>";
  }
echo "</table>";mysql_close($con);
?>
  <br />
  </p>
</body>
</html>

 

The testphp2.php is a linked file that submits new data into the database. Now in order for me to submit the loaded values directly into the DB after editing I need to tell SQL which row to insert those values into, now how do I do that ? My only idea was this...I'd start with an empty DB again and create a test_userinfo table.

 

Then I'll create all the rows I have now name, lastname, age, email, location, comments. Then I was thinking of creating another row and call it "record_num" that would represent the number of the record in the database.

 

In the PHP file I would create a hidden input that would load the numerical value of all the rows existing in that table and when submitting the new information it would add 1 to that number and file that number as the record number for that entry.

 

Am I thinking right about this or do table rows already have these record numbers assigned automatically ?

 

The only reason I even came up with that idea is because when the page loads and all these values show up in the input boxes I want to be able to edit them and write them right back to DB, but I need to tell the DB which record goes where and I am not sure if these ROWS have names or numbers by default.

 

Basically when I edit records I want to use something like this :

 

mysql_query("UPDATE test_userinfo SET lastName = 'New Last Name'
WHERE record_Num = '01 or whatever number the record is' ");

 

I hope i didn't confuse you too much, but let me know if I'm going about this right or not. Thank you in advance guys. This isn't really a project that needs to be practical, it's just something I'm doing to figure out the workings of PHP and SQL and play around with it.

Link to comment
Share on other sites

Hi, welcome to PHP Freaks!

 

I created just the thing you are trying to do at the request of another member. I suggest you read through that thread, download the code (2 separate pages) and study it. It will answer almost every question you posed here, and then you can modify your code accordingly.

 

http://www.phpfreaks.com/forums/index.php/topic,166794.0.html

 

PhREEEk

Link to comment
Share on other sites

Oh wow it is pretty much word for word the same thing. Thanks a lot ! I'll start reading all that ( probably will take me a day to comprehend what is what in there haha ) but I'll post my questions here if I stumble on something.

 

One more thing I wanted to ask is are there any tutorials of sort where I can read on all the conditions and variables of SQL ( i'm not sure if that's what they are called or not ) but basically stuff like $col_name and all the other strings is there like a big list of what they all mean and do ? I was looking around the net for good resources on how to use PHP to control SQL but haven't found much usefull stuff except maybe the W3 website. I imagine it'll probably take me forever to learn PHP ( speaking of how do people learn all this stuff, I know designers in their 20's that know everything from ASP to PHP to SQL and CF, seems like it would take some time to learn these things, but somehow a lot of people know all these languages as if it takes a month or two. )

 

Anyway, I'd appreciate any guides or references you could recommend, I'm giving PHP and my brain a shot, I know I can learn it, just not sure where to start.

 

Thanks a bunch. Cheers.

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.