View Full Version : Help with PHP MYSQL Hyperlinks and Forms

08-26-2009, 01:25 PM

I want to use a hyperlink to load details for a specific record into an updatable form page and then use that form page to update that record in a mysql database.

I have 2 php pages. The first returning the bet_id of the last 10 records of a MYSQL query and the second returning all field values for a specific record into a form, giving the end user the opportunity to update the field values. I can link the two so that when I click on say row 3 (bet_id = 3) of the table in the first page it takes me to the second page using the bet_id '3' in the MYSQL query utilised by the second page, to prepopulate the form fields with the correct values.

What I am having problems with is updating the database with any new values I enter in the form fields when I hit the 'Update bet' button in the form. The fields I am showing in the page 2 form are 'punter_id' and 'category_id'. Can anyone suggest what might be going wrong? I am not getting any errors when I hit the 'Update bet' button, but the form field values just revert back to what they were originally, and the database table does not get updated by the query declared in $query (see below). Sorry if it's a little messy. Am v. new to PHP.

Code included below:

<?php require_once('../Connections/punters_c.php'); ?>

mysql_select_db($database_punters_c, $punters_c);

$query_Recordset1 = "SELECT bet_id, punter_id,category_id FROM betslip where bet_id =".intval($_REQUEST['bet_id']);
$Recordset1 = mysql_query($query_Recordset1, $punters_c) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
return $theValue;
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

if ((isset($_POST["apply"])) && ($_POST["apply"] == "update_betslip_detail")){

$query = sprintf(" UPDATE betslip
SET category_id = '%d'
WHERE bet_id = %d",

mysql_select_db($database_punters_c, $punters_c);
$Result1 = mysql_query($query, $punters_c) or die('Connection error to MYSQL occurred: '.(mysql_error()));

header("Location: /update_betslip_test.php");

echo "bet detail not updated";

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>

<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="update_betslip_detail">

<input type="text" name="bet id" id = "bet_id" value="<?php echo $row_Recordset1['bet_id']; ?>"/>
<input type="text" name="category_id" id = "category_id" value="<?php echo $row_Recordset1['category_id']; ?>"/>

<input type="hidden" name= "apply" value="update_betslip_detail"/>

<input type="submit" value="Update bet"/>

<p><a href="update_betslip_test.php">Back to Update page </a></p>

08-27-2009, 01:08 AM
yes confusing code as usual with dw wizard stuff
first step to de bug, echo the values (after the form) to see if they are being sent

08-27-2009, 12:00 PM
OK. I get the altered value when I change the category_id value and hit the Update Bet button, so all good on that front. Whats the next step?

echo ($_POST['category_id']);

08-28-2009, 12:43 AM
also is
update_betslip_detail thee value returned from $_POST["apply"]?

08-28-2009, 10:49 PM
Sorry not quite following what you're asking. I definitely want to use the values posted by apply as the category_id and punter_id variables in the Update query.

08-31-2009, 02:21 AM
i was asking if the value returned by $_POST["apply"] is the value you want to use as update_betslip_detailas in
((isset($_POST["apply"])) && ($_POST["apply"] == "update_betslip_detail"
just to be sure the value is correct

08-31-2009, 11:48 AM
Yep, thats correct. Any ideas as to what I'm doing wrong?