[SOLVED] Inserting data into MySQL using PHP

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
concatenate_man
Forum Newbie
Posts: 13
Joined: Tue Dec 09, 2003 1:22 am
Location: Australia

[SOLVED] Inserting data into MySQL using PHP

Post by concatenate_man »

Hi folks, I am having trouble with some of my php code when it comes to inserting data in mysql. First there is a page called insert_buyer.html that is a form for gathering required data

Code: Select all

<html>
<head>
	<title>MarCat Buyer Insertion</title> 
</head>
<body bgcolor="#000000" text="#FFFFFF" link="#0000FF"
     vlink="#000080" alink="#FF0000">
    <p align="center"><img src="images/logo.gif" width="850" height="89"></p>
<p>
<h1>MarCat Buyer Insertion Page</h1>
<p>
<hr>
<p>
<form action="insert_buyer.php" method="post">
  <table border="0">
    <tr>
      <td>NAME</td>
      <td><input type="text" name="buyer_name" maxlength="20" size="20"><br/></td>
    </tr>
    <tr>
      <td>ADDRESS</td>
      <td><input type="text" name="buyer_address" maxlength="50" size="50"><br/></td>
    </tr>
    <tr>
      <td>PHONE</td>
      <td><input type="text" name="buyer_phone" maxlength="20" size="20"><br/></td>
    </tr>
    <tr>
      <td>COMPANY</td>
      <td><input type="text" name="buyer_company" maxlength="50" size="50"><br/></td>
    </tr>
    <tr>
      <td>ABN</td>
      <td><input type="text" name="buyer_abn" maxlength="20" size="20"><br/></td>
    </tr>
    <tr>
      <td colspan="2"><input type="submit" value="Insert Buyer"</td>
    </tr>
  </table>
</form>
</body>
</html>
this calls insert_buyer.php

Code: Select all

<html>
<head>
	<title>MarCat Buyer Insertion</title> 
</head>
<body bgcolor="#000000" text="#FFFFFF" link="#0000FF"
     vlink="#000080" alink="#FF0000">
    <p align="center"><img src="images/logo.gif" width="850" height="89"></p>
    <p align="center"><h1>Buyer Insertion Results</h1>
<p>
<hr>
<?php 

$buyer_name=$HHTP_POST_VARS&#1111;'buyer_name'];
$buyer_address=$HHTP_POST_VARS&#1111;'buyer_address'];
$buyer_phone=$HHTP_POST_VARS&#1111;'buyer_phone'];
$buyer_company=$HHTP_POST_VARS&#1111;'buyer_company'];
$buyer_abn=$HHTP_POST_VARS&#1111;'buyer_abn'];

if (!buyer_name || !buyer_address || !buyer_phone || !buyer_company || !buyer_abn)
&#123;
	echo 'You have not entered all the required details.<br/>'
	     .'Please go back and try again...';
	exit;
&#125; 

$buyer_name = addslashes($buyer_name);
$buyer_address = addslashes($buyer_address);
$buyer_phone = addslashes($buyer_phone);
$buyer_company = addslashes($buyer_company);
$buyer_abn = addslashes($buyer_abn);


$connection = mysql_connect('localhost');

if (!$connection)
&#123;
	echo 'Error: Could not connect to database. Please try again later.';
	exit;
&#125;

mysql_select_db('marcat');

$query = "insert into BUYERS(Name,Address,Phone,Company,ABN) values
	  ('".$buyer_name."','".$buyer_address."','".$buyer_phone."','".$buyer_company."','".$buyer_abn."')";

$result = mysql_query($query);

if ($result)
	echo mysql_affected_rows().' buyer inserted into database.';

?>

</body>
</html>
now when i run this it creates a new row in the BUYERS but I only get the default values eg nothing for varchars and ) for int. can anyone tell me what i am doing wrong and how i can fix this situation.

cheers
Joe
User avatar
aquila125
Forum Commoner
Posts: 96
Joined: Tue Dec 09, 2003 10:39 am
Location: Belgium

Post by aquila125 »

HHTP ? Must be a new protocol :)

It should be HTTP_POST_VARS (deprecated).. these days you should use the $_POST array..
concatenate_man
Forum Newbie
Posts: 13
Joined: Tue Dec 09, 2003 1:22 am
Location: Australia

Post by concatenate_man »

aquila125, thank you, thank you and thank you. yes that fixed the problem. I had been looking at this code for about 4 hrs and it was starting to drive me crazy.....
anytime you are in Brisbane Australia I'll buy you a beer...
Cheers mate,
Joe
Post Reply