Inserting form data to DB/ mysql<

admin

Administrator
Staff member
I've been searching for days now, and I can't seem to get it right. I've used others' examples.. I've used tutorials and nothing helps. So now I come here in hopes you can help me.

I have a small script for inserting my DVD collection into a database.

Here's the PHP code of what I have so far, if this is even correct. I've seen different versions and ways of inserting data from forms to a database. Maybe somone can point me in the right direction..
---------------------------

<?php

// connect to the DB

$db = mysql_connect('127.0.0.1', 'mysql', "") or die("Could not login");

// Selects the database

mysql_select_db('db_dvdlist', $db) or die('Can\'t use database : ' . mysql_error());

// Inserts the record

mysql_query ("INSERT INTO dvd ('dvdName', 'dvdPrice')
VALUES ('$dvdName', '$dvdPrice')");

print ("Data entered successfully");


?>

-----------------------
And here's my HTML Form.
-----------------------

<form action="dvdpost.php" method="post">
<table align="center" border="1" cellpadding="2" cellspacing="4">
<tr>
<td align="center">
<b>DVD Name</b>:<br />
<input type="text" name="dvdName" maxlength="25" size="25">
<br /><br />
<b>DVD Price</b>:<br />
<input type="text" name="dvdPrice" maxlength="5" size="4">
<br /><br />
<input type="submit" value="Submit">
<input type="reset" value="Reset">

</td>
</tr>
</table>
</form>

--------------------------

So what am I doing wrong?

my database name is 'db_dvdlist'
table name is 'dvd'
I also have inside the database an auto-incrementing ID number used as the Primary Key.
Am I suppose to include this inside the form as well (maybe as a hidden value?) Or do I not even need it? I'd like to use it.. but if I don't need it....

Thanks in advance.
Kennywell you have the right idea. but you need to change a few things. look at the code and see my changes. the reason for those is that your php setup has register_globals off which is a security setting.


<?php

// connect to the DB

$db = mysql_connect('127.0.0.1', 'mysql', "") or die("Could not login");

// Selects the database

mysql_select_db('db_dvdlist', $db) or die('Can\'t use database : ' . mysql_error());

// Inserts the record

mysql_query ("INSERT INTO dvd ('id','dvdName', 'dvdPrice')
VALUES ('','{$_POST['dvdName']}', '{$_POST['dvdPrice']}')");

print ("Data entered successfully");


?>
 
Back
Top