Help - Search - Members - Calendar
Full Version: Update Database With Php
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
MrTouz
Hello, again its me.. in trouble...

i searched the web and found what i needed but never could apply it to my script.
I am looking for a way to update some database information i have with a script. The thing is that i have a script that adds info to the database (works nice) i have a script which displays the info to the database (works nice) and now i need a script that updates/modify info from the database.

I don't really mind how to modify the info / either by having a button next to each of the 'users' saying Update Info ? or a page where it asks which user 'id' i want to update, i don't mind, i just need to update info via my website without going to phpmyadmin.

This is what i have setup (don't laugh.. its pretty simple, nothing to fancy... coming from w3school php section tongue.gif)

Insert.php which inserts data from the form.

CODE
<?php
include 'connect.php';
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$sql="INSERT INTO clientinsert (nom, prenom, email, datedn, ville, codepostal, rue, plan, achat, fin, extras)
VALUES
('$_POST[nom]','$_POST[prenom]','$_POST[email]','$_POST[datedn]','$_POST[ville]','$_POST[codepostal]','$_POST[rue]','$_POST[plan]','$_POST[achat]','$_POST[fin]','$_POST[extras]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Client Added";

mysql_close($con)
?>


clients.php which shows the different users / clients infos that i inserted with the form :

CODE
<?php
include 'connect.php';
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

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

echo "This is a list of all our clients.";

while($row = mysql_fetch_array($result))
  {
  echo "
  <table width=\"520\" border=\"0\">
    <tr>
        <td valign=\"top\" width\=260\">
            <table border=\"0\">
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Nom :</b></td><td width=\"140\">" . $row['nom'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Prénom :</b></td> <td width=\"140\">" . $row['prenom'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Email :</b></td><td width=\"140\">" . $row['email'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>ID :</b></td> <td width=\"140\">" . $row['id'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Date dn :</b></td><td width=\"140\">" . $row['datedn'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Code postal :</b></td> <td width=\"140\">" . $row['codepostal'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Ville :</b></td><td width=\"140\">" . $row['ville'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Rue :</b></td> <td width=\"140\">" . $row['rue'] . "</td>
                </tr>
            </table>
        </td>
        <td valign=\"top\">
            <table border=\"0\" width\=260\">
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Plan :</b></td><td width=\"140\">" . $row['plan'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Achat :</b></td> <td width=\"140\">" . $row['achat'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Fin :</b></td><td width=\"140\">" . $row['fin'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Extras :</b></td> <td width=\"140\">" . $row['extras'] . "</td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<hr>";
  }
echo "";

mysql_close($con);
?>



Any info would be awsome, please while you provide info if you could give an example... i really am a noob at this :/
Gawdl3y
I'm pretty sure that you need to use the mysql query function, "UPDATE".
I believe it goes something like this:
CODE
mysql_query("UPDATE tablename SET columnname=columnvalue WHERE clientid=CLIENTID");

And inside the form that changes it, you would add:
CODE
echo '<input type="hidden" name="id" value="'. $_GET['id'] .'" />';

And to the URL of the modify form, the link would be something like:
CODE
echo '<a href="?action=modify&id='. $id .'"></a>';


You could switch it up a bit and use the client name instead of id or something like that.


Tell me if you're confused. smile.gif


(I didn't actually physically make the code for you because I want you to learn. smile.gif )
MrTouz
Well thing is i am scratching my head for the past 3 hours, i got the part where it says i need to use the UPDATE andwhere i haveto put the hidden... but i just can't put it all togheter to create one line of freaking code.

Gawdl3y
Change your Clients.php to this (FIRST MAKE A BACKUP IN CASE MY CODING IS FLAWED):
CODE
<?php
include 'connect.php';
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

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

if(!isset($_GET['edit'])) {
echo "This is a list of all our clients.";

while($row = mysql_fetch_array($result))
  {
  echo "
  <table width=\"520\" border=\"0\">
    <tr>
        <td valign=\"top\" width\=260\">
            <table border=\"0\">
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Edit User :</b></td><td width=\"140\"><a href=\"?edit&id=". $row['id'] ."\">Here</a></td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Nom :</b></td><td width=\"140\">" . $row['nom'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Prénom :</b></td> <td width=\"140\">" . $row['prenom'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Email :</b></td><td width=\"140\">" . $row['email'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>ID :</b></td> <td width=\"140\">" . $row['id'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Date dn :</b></td><td width=\"140\">" . $row['datedn'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Code postal :</b></td> <td width=\"140\">" . $row['codepostal'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Ville :</b></td><td width=\"140\">" . $row['ville'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Rue :</b></td> <td width=\"140\">" . $row['rue'] . "</td>
                </tr>
            </table>
        </td>
        <td valign=\"top\">
            <table border=\"0\" width\=260\">
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Plan :</b></td><td width=\"140\">" . $row['plan'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Achat :</b></td> <td width=\"140\">" . $row['achat'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Fin :</b></td><td width=\"140\">" . $row['fin'] . "</td>
                </tr>
                <tr>
                    <td valign=\"top\" width=\"105\"><b>Extras :</b></td> <td width=\"140\">" . $row['extras'] . "</td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<hr>";
  }
} else {
  $sql = "SELECT * FROM clientinsert WHERE id=". $_GET['id'];
  $result = mysql_query($result);
  $CountID = mysql_count_rows($result)

  if(!isset($_GET['id']) || empty($_GET['id']) || $CountID == 0)
    die("Invalid ID Specified.");

  $ClientArray = mysql_fetch_array($result);
// START EDITING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

// This is just an example...
echo '<input type="text" name="name" value="'. $ClientArray['name'] .'" />';


// END EDITING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
echo "";

mysql_close($con);
?>


And edit that area. You can probably use the same form that you do for adding a client, just change the form action to the file that adds them, and add in the form that you use to add and change each text field to the $ClientArray['VALUE'].

Hope it helps, man. wink.gif
MrTouz
Parse error: syntax error, unexpected T_IF in /homepages/23/d222196706/htdocs/mtz/sss/switch.php on line 74

Weird, been searching and can't find what causes this :/

btw this is the line :
CODE
  if(!isset($_GET['id']) || empty($_GET['id']) || $CountID == 0)
    die("Invalid ID Specified.");


and it looks just fine :/
Gawdl3y
MY BAD...

add a semicolon after the "$CountID = mysql_count_rows($result)"

Sorry. tongue.gif
MrTouz
Wow looks like it does not want to work tongue.gif

CODE
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /homepages/23/d222196706/htdocs/mtz/sss/switch.php on line 83


line 83 :

CODE
<td>Name :</td> <td><input type=\"text\" name=\"nom\" value=\"'. $ClientArray['nom'] .'\" /></td>



sorry, find the error my mistake, i had ro replace echo " by echo ' just like you did :/
Gawdl3y
Make sure you're escaping the quotes correctly. If you use echo ', you only need to escape the single quotes ('), and if you use echo ", you only need to escape the double quotes. (")
MrTouz
CODE
Fatal error: Call to undefined function mysql_count_rows() in /homepages/23/d222196706/htdocs/mtz/sss/switch.php on line 73


Weird :/

$CountID = mysql_count_rows($result);
Gawdl3y
Again, oops.
See, even advanced PHP'ers mess up.

Change that to mysql_num_rows
MrTouz
Problem(s) solved and thanks to Gawdl3y

Really big thanks for helping me out for the next 4 hours tongue.gif you did an awsome job !
Gawdl3y
Ty, man. biggrin.gif
Bogey
If a regular user visits that page that user would be able to edit the user's information simply by adding &id=#... you might want to have a log-in type of thing to make sure only you can do that.
MrTouz
its actually what i have.
Before you can make it to te path of the my acp you have to enter a login and a password. Might not be best of protection but it protects any pages on that specific location. which s enough for what i need.

Its only for my use. the path is unknown, i mean its not a link that i share. I just want to test databse info use. I am wanting to add sessions now o it combines both scripts i have. login and user database
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.