Random Pick and Update
Hi guys, I'm just starting to learn PHP. I need a help with this one... I made a random pick of file from my database. What I need to do is make the page able to update the field "agent" and "comments" of the selected file. Each randomly selected file can be edited. Can someone help me how to do it or just give me a good tutorial similar to this? Thanks a lot...
Anyway here is the random pick php script
<?
$username="myusername;
$password="mypassword";
$database="agents";
$link = mysql_connect(mysql,$username,$password);
if ($link) {
mysql_selectdb("agents",$link);
// Select records from the DB
$query = "SELECT * FROM clients ORDER BY Rand() LIMIT 1";
$result = mysql_query($query);
// Display records from the table
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "Client: $row[1]Agent: $row[2]Last Comment: $row[3]";
}
echo "";
} else {
echo "Can't connect to the database!";
}
?>
Busy posted this at 20:53 — 22nd May 2007.
He has: 6,151 posts
Joined: May 2001
If I got it right you just need to display the content from the database into form fields (input, textarea etc), then on submission of the form be sure to validate the information before updating the database, including addslashes to any content that allows them.
To get just the details of the one you want change this line in the above code
echo "Client: $row[1]Agent: $row[2]Last Comment: $row[3]";
to
echo "Client: ".$row[1]."Agent: $row[2]Last Comment: $row[3]";
which will make the clients name clickable. I have added $row[id] in hoping you are using an id in your database, if you are not, do, and make it unique and autoincrement
bluenote posted this at 21:28 — 22nd May 2007.
He has: 10 posts
Joined: Dec 2005
Thanks for the reply busy. Sorry maybe I didn't explain it right... Here is the link of the page I want to modify...
http://elitepromotionalservices.com/try.php
Every time the "Random Pick" link was click, it will display a random file... What I need to do is when the form with "Agent" and "Comment" filled up was submitted the current file displayed will be updated. The only fields I need to change was the agent and comment... I hope I explained it right. Thanks!!!
Busy posted this at 09:35 — 23rd May 2007.
He has: 6,151 posts
Joined: May 2001
I went to your page and the deatils shown are:
Client: Eanna Pitsburgh
Agent: Drinn
Last Comment: Call me again tomorrow...
So your wanting to change the Agent and Last Comment for the Client, who in this case is Eanna Pitsburgh when the form is submitted?
If this is the case you need an id number (in the database and as a hidden form field) so on submission you just
update clients set agent=$_POST[agent], comment=$_POST[comment] where id=$_POST[id]
this will replace the content in the database, if you wanted to just add another row of content then use 'insert into' rather than 'update'.
you could use the agent name instead of the id but would run into problems with spaces in the name and dupilcates.
The code shown is only the last bit, you should of course validate the code and make sure it is there, is valid text without any symbols, script code, quotes or anything else that could destory your databases content/structure
bluenote posted this at 10:45 — 23rd May 2007.
He has: 10 posts
Joined: Dec 2005
Sorry but I'm absolutely new to php... how will that code recognized the id from the first php script(the random pick).....
Should i insert your given script with the try.php(random pick) or would I create a new php file that will be called by the form action? What I did was like that, I create a php file called "update.php"
here it is
<?
$username="myusername";
$password="mypassword";
$database="agents";
$agent=$_POST['agent'];
$comment=$_POST['comment'];
$id=$_POST['id'];
mysql_connect(mysql,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "UPDATE clients set agent=$post[agent], comment=$post[comment] where id=$post[id]";
mysql_query($query);
mysql_close();
?>
and then I set the form to action="update.php" but it didnt work. How will I update the selected file? Im so sorry im really new with php. Thanks again...
Greg K posted this at 20:27 — 23rd May 2007.
He has: 2,145 posts
Joined: Nov 2003
A couple of things:
I looked at your page with the form. You need to actually put the ID of the record in the form, you have it set to just "ID".
A sample would be like this, assuming that the row ID is set in a fiend called `ID`, and the client name is in a field called `client`
<?php
$dbServer = \"127.0.0.1:3307\";
$dbUser = \"myusername\";
$dbPass = \"mypassword\";
$dbName = \"agents\";
$dbLink = mysql_connect($dbServer, $dbUser, $dbPass)
or die(\"Could not connect to database server\");
mysql_selectdb($dbName,$dbLink)
or die(\"Could not access database\");
$dbQuery = \"SELECT * FROM `clients` ORDER BY Rand() LIMIT 1\";
$dbResult = mysql_query($dbQuery)
or die('Invalid query: ' . mysql_error());
$dbRow = mysql_fetch_assoc($dbResult);
<p><a href=\"try.php\">Random Pick</a></p>
<form id=\"form1\" name=\"form1\" method=\"post\" action=\"update.php\">
<p>
Agent:
<input name=\"agent\" type=\"text\" id=\"agent\" value=\"= $row['agent'] \" />
</p>
<p>Comment:</p>
<p>
<textarea name=\"comment\" rows=\"4\" id=\"comment\">= $row['comment'] </textarea>
</p>
<p>
<input type=\"submit\" name=\"Submit\" value=\"Submit\" />
</p>
<input type=\"hidden\" name=\"id\" value=\"= $row['ID'] \" />
</form>
<p>
Client: = $row['client'] <br />
Agent: = $row['agent'] <br />
Comment: = $row['comment']
</p>
?>
Some notes for this one:
Note the reloaction/addition of the DIE statements to deal with any possible connection problems. It is a good habbit to get into, doing this way keeps the 'error' messages right by what is generating them instead of in an ELSE after all the other code.
I eliminated the while loop, since it is hard coded to only have one record, there is no need for it.
You originately got the row result into an array that is numbered, however IMO using mysql_ fetch_ assoc is better for two reasons: 1. the code is easier to read/user ($row['agent'] instead of $row[2]) and 2. if for some reason you modify the database and add/remove fields, your numbering scheme would be off)
Lastly, I had the form auto fill in the current agent/comment data. If you choose to not have this, make sure you still *leave* the code alone in the hidden input line.
Then on your update.php page, try something like:
<?php
// NOTE you should do more error checking on the values that come in through $_POST
$agent = $_POST['agent'];
$comment = $_POST['comment'];
$id = $_POST['id'];
$dbServer = \"127.0.0.1:3307\";
$dbUser = \"myusername\";
$dbPass = \"mypassword\";
$dbName = \"agents\";
$dbLink = mysql_connect($dbServer, $dbUser, $dbPass)
or die(\"Could not connect to database server\");
mysql_selectdb($dbName,$dbLink)
or die(\"Could not access database\");
$dbQuery = \"UPDATE `clients` SET `agent`='\" . $agent . \"', `comment`='\" . $comment . \"' WHERE `id`=\" . $id;
$dbResult = mysql_query($dbQuery)
or die('Invalid query: ' . mysql_error());
<p>Record Updated</p>
?>
Hope these help, and if they don't, to help further figuring it out, please post your table structure.
-Greg
bluenote posted this at 09:27 — 25th May 2007.
He has: 10 posts
Joined: Dec 2005
Here's the error i get... And the page with the form, I didnt get any result from the random pick anymore.
Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
bluenote posted this at 01:28 — 26th May 2007.
He has: 10 posts
Joined: Dec 2005
Okay I got it functioning well now... The error is the "row" it should be "dbRow"
Lots of lots of thanks GREG K!!!!
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.