Web Hosting Talk







View Full Version : PHP Help


ruleitho
06-06-2006, 04:18 PM
PHP definatly isn't my stong point, so i was wondering if anyone could help me with this...

file1.php
<?PHP

// Database Details

$host = "localhost";
$database = "**********";
$user = "*********";
$password = "******";

// MySql Connection
$connection = mysql_connect($host,$user,$password)
or die ("$Error1");
$db = mysql_select_db($database,$connection)
or die ("$Error2");

// Error Messages

$Error1 = "Sorry No Information Available [Error: 1]"; // Check Database Host, Username and Password
$Error2 = "Sorry No Information Available [Error: 2]"; // Check Database Name
$Error3 = "Sorry No Information Available [Error: 3]"; // Unable to Execute Query for Today

$day = date("Y-m-d", mktime(0,0,0, date(m), date(d)-1,date(Y)));


// Queries

$detail_query = "SELECT id,date,detail FROM specialoffers WHERE date>'$day' LIMIT 30";


// Query and Database

$result = mysql_query($detail_query)
or die ("ERROR 111");


echo "<form method='POST' action='process.php'>\n";
echo "<p>&nbsp;</p>";
echo "<table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='476'>";

while ($row = mysql_fetch_array($result))
{
extract($row);
echo "<tr>\n
<td width='150' align='center'>$date</td> \n
<td width='288'>\n
<p align='center'><textarea rows='6' name='$date' cols='27'>$detail</textarea></td>\n
</tr>\n";
}


echo " </table>
<p>&nbsp;</p>
<p><input type='submit' value='Submit'><input type='reset' value='Reset' name='B2'></p>
</form>";

?>
process.php
...
$date = trim($date);
$query = "UPDATE specialoffers SET detail='$date' WHERE date='$date'";

$results = mysql_query($query)
or die ("Couldn't Update Information...");

?>

Database Structre

id | _date__ | ____detail ____ |
1 | 2006-1-1 | blah blah blah xc|
2 | 2006-1-2 | blah blah blahad |
3 | 2006-1-3 | blah blah sablah |
4 | 2006-1-4 | blah blahae blah |
5 | 2006-1-5 | blah blahad blah |
...


file1.php displays 30 records from the database, the first record being todays date. The results are displayed so the detail appears in a text box so it can be altered and updated in the database.

process.php is ment to update the information in the database, however it's not working. I get no error, but the database isn't updating. Can you take a look at the code and see where i'm going wrong...

ThatScriptGuy
06-06-2006, 05:05 PM
process.php, make sure and pull the items from the $_POST instead of accessing them directly. Maybe that's your problem?
Kevin

Oras
06-06-2006, 05:30 PM
for checking ... print the query before processing it:

...
$date = trim($date);
$query = "UPDATE specialoffers SET detail='$date' WHERE date='$date'";
echo $query;

$results = mysql_query($query)
or die ("Couldn't Update Information...");

AdamKDean
06-06-2006, 06:30 PM
You have to define vars before you can use them. So swap these around like so:

// Error Messages

$Error1 = "Sorry No Information Available [Error: 1]"; // Check Database Host, Username and Password
$Error2 = "Sorry No Information Available [Error: 2]"; // Check Database Name
$Error3 = "Sorry No Information Available [Error: 3]"; // Unable to Execute Query for Today

// MySql Connection
$connection = mysql_connect($host,$user,$password)
or die ("$Error1");
$db = mysql_select_db($database,$connection)
or die ("$Error2");

ruleitho
06-06-2006, 08:40 PM
process.php, make sure and pull the items from the $_POST instead of accessing them directly. Maybe that's your problem?
Kevin

Sorry I don't understand what you mean? can you post can example?


for checking ... print the query before processing it:

...
$date = trim($date);
$query = "UPDATE specialoffers SET detail='$date' WHERE date='$date'";
echo $query;

$results = mysql_query($query)
or die ("Couldn't Update Information...");


I get "UPDATE specialoffers SET detail='POST_' WHERE date='POST_' " which must be what kvnband is taking about, but I don't understand the correct code to fix it?


You have to define vars before you can use them. So swap these around like so:

// Error Messages

$Error1 = "Sorry No Information Available [Error: 1]"; // Check Database Host, Username and Password
$Error2 = "Sorry No Information Available [Error: 2]"; // Check Database Name
$Error3 = "Sorry No Information Available [Error: 3]"; // Unable to Execute Query for Today

// MySql Connection
$connection = mysql_connect($host,$user,$password)
or die ("$Error1");
$db = mysql_select_db($database,$connection)
or die ("$Error2");

Yer, don't know why I put them that way round, its me not thinkin, its not a connection error anyways

ruleitho
06-06-2006, 09:27 PM
Ok got it to work using teh following code in process.php...



foreach ($HTTP_POST_VARS as $key => $value)
{
$value = trim($value);
$query = "UPDATE specialoffers SET detail='$value' WHERE date='$key'";
$results = mysql_query($query)
or die ("Couldn't Update Information...");

}


Thanks Everybody...

nnormal
06-08-2006, 03:50 PM
no don't do that
baby j will :bawling:


http://us2.php.net/manual/en/tutorial.forms.php




if(isset($_POST['nameofthefield']))
$nameofthefield = $_POST['nameofthefield'];
else
$nameofthefield = false; // or "" if you like that better..