Code: Select all
<?php
if ($_POSTїop] != "add") {
//haven't seen the form, so show it
$display_block = "<h1>Add a Book</h1>
<form method="post" action="$_SERVERїPHP_SELF]">
<P><strong>Title of Book:</strong><br>
<input type="text" name="item_name" size=50 maxlength=100>
<P><strong>Author of Book:</strong><br>
<input type="text" name="item_author" size=30 maxlength=75>
<P><strong>Artist:</strong><br>
<input type="text" name="item_author" size=30 maxlength=75>
<P><strong>Description of Book:</strong><br>
<textarea name="item_desc" cols=35 rows=5 wrap=virtual></textarea>
<P><strong>Price of Book:</strong><br>
<input type="text" name="item_price" size=10 maxlength=10>
<!-- The data encoding type, enctype, MUST be specified as below -->
<method="POST" action="addimage.php" enctype="multipart/form-data">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Save this Image: <input name="userfile" type="file" />
<input type="submit" value="Save File" />
<p><input type="submit" name="submit" value="Add Book"></p>
</FORM>";
} else if ($_POSTїop] == "add") {
//time to add to tables, so check for required fields
if ($_POSTїitem_name] == " ") {
header("Location: addentry.php");
exit;
}
//connect to database
$conn = mysql_connect("localhost", "username", "password")
or die(mysql_error());
mysql_select_db("shoot_the_moon_store",$conn) or die(mysql_error());
//add to store_items table
$add_title = "insert into store_items values ('', now(), now(),
'$_POSTїitem_name]')";
mysql_query($add_title) or die(mysql_error());
//get item_id for use with other tables
$item_id = mysql_insert_id();
if ($_POSTїitem_author]) {
//something relevant, so add to store_item_author table
$add_author = "insert into store_item_author values ('', $item_id,
now(), now(), '$_POSTїitem_author]')";
mysql_query($add_author) or die(mysql_error());
}
if ($_POSTїitem_artist]) {
//something relevant, so add to store_item_author table
$add_artist = "insert into store_item_artist values ('', $item_id,
now(), now(), '$_POSTїitem_artist]')";
mysql_query($add_artist) or die(mysql_error());
}
if ($_POSTїitem_desc]) {
//something relevant, so add to store_items table
$add_desc = "insert into store_items values ('', $item_desc,
now(), now(), '$_POSTїitem_desc]')";
mysql_query($add_desc) or die(mysql_error());
}
if ($_POSTїitem_price]) {
//something relevant, so add to store_items table
$add_price = "insert into store_items values ('', $item_price,
now(), now(), '$_POSTїitem_price]')";
mysql_query($add_price) or die(mysql_error());
}
$display_block = "<h1>Record Added</h1>
<P>Your record has been added. Would you like to
<a href="addbook.php">add another</a>?</p>";
}
?>
<HTML>
<HEAD>
<TITLE>Add a Book Record</TITLE>
</HEAD>
<BODY>
<?php echo $display_block; ?>
</BODY>
</HTML>What should my code be for allowing a user to add the link of an image to the database?
Can I have a form embedded within a form to allow me to do this?
Is the rest of the code correct?, when I tried it before it didn't save the data into the database, and just refreshed the page.