PHP MYSQL Hyperlinks and Form Fields

Updating database with altered form field values

Page 1 of 1

3 Replies - 2126 Views - Last Post: 24 August 2009 - 04:13 AM Rate Topic: -----

#1 A_Sanders@tpg.com.au  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-June 09

PHP MYSQL Hyperlinks and Form Fields

Posted 24 August 2009 - 03:31 AM

Hi,

I want to use a hyperlink to load details into an updatable form page.

I have 2 php pages. One returning the bet_id of the last 10 records of a MYSQL query and another 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 new values I have entered 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).

Code included below:
<?php require_once('../Connections/punters_c.php'); ?>
<?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";
	  break;	
	case "long":
	case "int":
	  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
	  break;
	case "double":
	  $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
	  break;
	case "date":
	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
	  break;
	case "defined":
	  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
	  break;
  }
  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_real_escape($_POST['category_id']),
		 				mysql_real_escape($_POST['bet_id'])
																);
		
  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");

	}
	else 
	{
		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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>

<body>
	<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"/>
	</form>
	
	<p><a href="update_betslip_test.php">Back to Update page </a></p>
</body></html>
<?php
mysql_free_result($Recordset1);
?>



Is This A Good Question/Topic? 0
  • +

Replies To: PHP MYSQL Hyperlinks and Form Fields

#2 tommyflint  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 679
  • Joined: 24-July 08

Re: PHP MYSQL Hyperlinks and Form Fields

Posted 24 August 2009 - 03:54 AM

what is %d in your update query?
Was This Post Helpful? 0
  • +
  • -

#3 A_Sanders@tpg.com.au  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-June 09

Re: PHP MYSQL Hyperlinks and Form Fields

Posted 24 August 2009 - 04:13 AM

View Posttommyflint, on 24 Aug, 2009 - 02:54 AM, said:

what is %d in your update query?


%d is an integer type specifier for the category_id and bet_id arguments which follow.
Was This Post Helpful? 0
  • +
  • -

#4 mattman059  Icon User is offline

  • Epic Awesomeness
  • member icon

Reputation: 15
  • View blog
  • Posts: 538
  • Joined: 23-October 06

Re: PHP MYSQL Hyperlinks and Form Fields

Posted 24 August 2009 - 04:13 AM

 $query = sprintf("	UPDATE betslip
					SET category_id = '%d'
					WHERE bet_id = %d",	
						mysql_real_escape($_POST['category_id']),
						 mysql_real_escape($_POST['bet_id'])



the %d is a formatting option for the sprintf function he calls..I've personally never used sprintf or printf in any of my queries....Basically %d formats the number as a decimal number %o and %h are also options if im not mistaken for Octal and Hex respectively
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1