Jump to content

Inserting Records


bravo14

Recommended Posts

Hi all

 

I have probable made a very simple error hopefully someone can help

 

I am trying to insert a record into a database using PHP

 

the form is built using the following code

 

<?php
echo ('<form action="insert_fine.php"><table width="65%" border="0">');
echo ('<tr>');
echo ('<td width="30%" class="profiletext">Name</td>');
echo ('<td><select name="name">');
$con = mysql_connect('IPaddress', 'database', 'password', true);
if(!$con)
{
      die('Could not connect: ' . mysql_error());
}
mysql_select_db('yardleyheroes', $con);
$res=mysql_query('SELECT * FROM `profiles` WHERE `profiletype` =\'player\'ORDER BY `name`');
if(mysql_num_rows($res)==0) 
echo('<option>there is no data in table..</option>');
else
{
while($row=mysql_fetch_assoc($res))
{
echo('<option value='.$row[profile_id].'>'.$row[name].'</option>');
}
}

echo ('</select></td>');
echo ('</tr>');
echo ('<tr>');
echo ('<td class="profiletext">Game</td><td><select name="match" width="145">');
$fixtures=mysql_query('SELECT * FROM `fixtures` ORDER BY `date`');
if(mysql_num_rows($fixtures)==0) 
echo('<option>there is no data in table..</option>');
else
{
while($matchrow=mysql_fetch_assoc($fixtures))
{
echo('<option value='.$matchrow[match_id].'>'.$matchrow.'|'.$matchrow[Opposition].'</option>');
}
}
echo ('</select></td></tr>');
echo ('<tr>');
echo ('<td class="profiletext">Amount</td>');
echo ('<td><input type="text" name="amount"></td>');
echo ('</tr>');
echo ('<tr>');
echo ('<td class="profiletext">Reason</td>');
echo ('<td><textarea name="reason" cols="50" rows="4"></textarea></td>');
echo ('</tr>');
echo ('<tr>');
echo ('<td> </td>');
echo ('<td><input type="submit" name="Submit" value="Submit"></td>');
echo ('</tr>');
echo ('</table>');
echo('</form>');
?>

 

And the process code is

<?php
$con = mysql_connect('IPaddress', 'username', 'password', true);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db('yardleyheroes', $con);$sql="INSERT INTO fines (profile_id, fine_amount, reason)
VALUES
('$_POST[name]','$_POST[amount]','$_POST[reason]')";if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo ('1 record added');
echo ('<table><tr><td>Name</td><td>Amount</td><td>Reason</td></tr>');
echo ('<tr><td>'.$_POST[name].'</td><td>'.$_POST[amount].'</td><td>'.$_POST[reason].'</td></tr></table>');
mysql_close($con)

?>

 

When processing the Insert, the code says that the record has been produced, but the data from the form does not get created.

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/109886-inserting-records/
Share on other sites

-You didn't specify a method in your form tag. You need to put method = 'post' in that tag if you wish to use $_POST array. 

 

- You may or may not also have issues with your vars in your query string.  Change it to this:

$sql="INSERT INTO fines (profile_id, fine_amount, reason) VALUES ('{$_POST['name']}','{$_POST['amount']}','{$_POST['reason']}')";

 

 

Link to comment
https://forums.phpfreaks.com/topic/109886-inserting-records/#findComment-563856
Share on other sites

p.s.-

 

I don't recommend putting posted vars directly into a query string.  you should at the very minimum do this:

 

foreach ($_POST as $key => $val) {
   $$key = mysql_real_escape_string($val);
}
$sql="INSERT INTO fines (profile_id, fine_amount, reason) VALUES ('$name','$amount','$reason')";

Link to comment
https://forums.phpfreaks.com/topic/109886-inserting-records/#findComment-563860
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.