 |
| PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 3rd, 2006, 12:37 PM
|
|
Registered User
|
|
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hlpe with foreach();
Quote:
|
quote:I have this form I'm developing where I I'm using 3tables, vehicle, lists, category... where all of them and JOINed... all others work fine but my entry form to storing the datas to database work fine except for my cat_id which is not recieving unique ID specified in cat's (<select>......</select>) label from the form I'm using to post all datas to the database... please I have the code and where you can see example...
|
Quote:
register_form
PHP Code:
<?php
session_start();
require_once 'inc/conn.php';
//require_once 'inc/jserror.php';
//include 'inc/usmotors_header.inc';
?>
<!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>
<title> Inventory Entry </title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<p>
<center>
<form method="post" action="register_control.php">
<table width="450" border="0" cellpadding="1" cellspacing="2" class="MytableDataTD">
<tr width="450" class="MytableCapTD">
<td height="58" colspan="2" valign="top" class="MytableColumnTD">
<b><i>Vehicle Inventory Entry</i></b></p>
<div style="text-align:center;width:400;font-family:Tahoma, Verdana, Arial; font-size:10px">
Welcome to US Motors Vehicle Inventory entry page. This page is only for
vehicles entry. Make sure the Stock number and VIN matches the hardcopy.</div></td>
</tr>
<tr>
<td width="217" height="22" align="right" valign="top" class="field">
<p>Vehicle Stock#</p></td>
<td width="373" valign="top" class="field">
<input name="stockno" type="text" maxlength="255" size="40" class="drop"> </td>
</tr>
<tr>
<td width="217" height="22" align="right" valign="top" class="field">
<p>Vehicle Model#</p></td>
<td width="373" valign="top" class="field">
<input name="model" type="text" maxlength="255" size="40" class="drop"> </td>
</tr>
<tr>
<td height="22" align="right" valign="top" class="field">
<p>Vehicle Make</p></td>
<td valign="top" class="field">
<input name="make" type="text" maxlength="255" size="40" class="drop"> </td>
</tr>
<tr>
<td height="22" align="right" valign="top" class="field">
<p>Vehicle Year</p></td>
<td valign="top" class="field">
<select name="year" size="1" class="drop">
<option value="" selected>Select a year.....</option>
<option value="">_____________</option>
<?php
for ($years=date("Y"); $years >=1995; $years--) {
echo "<option value=\"$years\">$years</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td height="22" align="right" valign="top" class="field">
<p>Vehicle Color</p></td>
<td valign="top" class="field">
<input name="vcolor" type="text" maxlength="255" size="25" class="drop"> </td>
</tr>
<tr><td height="22" align="right" valign="top" class="field">
Price</td>
<td valign="top" class="field">
<input name="price" type="text" maxlength="100" size="25" class="drop"><br />
<div class="red">Please, enter numbers e.g.(2300) not $2300.</div></td>
</tr>
<tr><td height="22" align="right" valign="top" class="field">
<p>Vehicle Categories</p></td>
<td valign="top" class="field">
<select name="cats[]" size="1" class="drop">
<option selected value="">Select Your File</option>
<option value="">---------</option>
<?php
$cats = mysql_query('SELECT ID, name FROM categories ORDER BY name ASC');
while ($cat = mysql_fetch_array($cats)) {
$cid = $cat['ID'];
$caname = htmlspecialchars($cat['name']);
echo("<option value='$cid'>$caname</option><br />\n");
}
$ok = @mysql_query($sql);
if ($ok) {
echo("Category number succefully entered");
} else {
echo("<p>Error inserting Vehicle into category $cat_ID: " .
mysql_error() . '</p>');
}
?>
</select></td>
</tr>
<tr>
<td height="22" align="right" valign="top" class="field">
<p>Mileage</p></td>
<td valign="top" class="field">
<input name="mileage" type="text" maxlength="100" size="25" class="drop">
<div class="red">Please, enter in .e.g 23,999 format </div></td>
</tr>
<tr>
<td height="22" align="right" valign="top" class="field">
<p>VIN#</p></td>
<td valign="top" class="field">
<input name="vin" type="text" maxlength="100" size="25" class="drop"> </td>
</tr>
<tr>
<td width="217" height="22" align="right" valign="top" class="field">
<p>URL for the car</p></td>
<td width="373" valign="top" class="field">
<input name="url" type="text" maxlength="255" size="40" class="drop"> </td>
</tr>
<tr>
<td colspan="2" align="center" valign="top" class="field" id="submitrow">
<input type="hidden" name="id" />
<input type="reset" value="Reset Form" class="drop">
<input type="submit" name="subscribe" value="Confirm Entry" class="drop"></td>
</tr>
</table>
</form></p>
</center>
</body>
</html>
<?php include 'inc/footer.inc'; ?>
registration_process
PHP Code:
<!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>
<title> Inventory Entry </title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<center>
<body>
<?php
require_once 'inc/conn.php';
// Define post fields into simple variables
$model = $_POST['model'];
$make = $_POST['make'];
$year = $_POST['year'];
$mileage = $_POST['mileage'];
$vcolor = $_POST['vcolor'];
$price = $_POST['price'];
$vin = $_POST['vin'];
$stockno = $_POST['stockno'];
$url = $_POST['url'];
$cats = $_POST['cats'];
/* Let's strip some slashes in case the user entered
any escaped characters. */
$model = stripslashes($model);
$make = htmlspecialchars($make);
$year = stripslashes($year);
$mileage = htmlspecialchars($mileage);
$vcolor = stripslashes($vcolor);
$price = stripslashes($price);
$vin = stripslashes($vin);
$stockno = stripslashes($stockno);
$url = htmlspecialchars($url);
echo "<center><table width='400'>";
// Error checking to validate each field for text entry...
if((!$model) || (!$make) || (!$stockno) || (!$vin)){
echo "<tr width='400'><td valign='middle'><div class=\"field\">You did not submit the following required information!</div></td></tr>";
if(!$model){
echo "<tr width='400'><td><div class=\"field\">Veichle Model is a required field. Please enter it below.</div></td></tr><br />";
}
if(!$make){
echo "<tr width='400'><td><div class=\"field\">Vehicle Make is a required field. Please enter it below.</div></td></tr><br />";
}
if(!$stockno){
echo "<tr width='400'><td><div class=\"field\">Stock# is a required field. Please enter it below.</div></td></tr><br />";
}
if(!$vin){
echo "<tr width='400'><td><div class=\"field\">VIN is a required field. Please enter it below.</div></td></tr><br />";
}
include 'reg_inventory.php';
exit();
}
$mileage = trim($_POST['price']);
if (!is_numeric($price)) {
echo "<tr width='400'><td><div class=\"field\">Price number format is not correct. Please try entring e.g Numbers</div></td></tr><br />";
include 'reg_inventory.php';
exit();
}
if (!is_string($stockno)) {
echo "<tr width='400'><td><div class=\"field\">Stock number format is not correct. Please try entring e.g Numbers</div></td></tr><br />";
include 'reg_inventory.php';
exit();
}
// Check for existing user with the new id
$sql_VIN_check = mysql_query("SELECT DISTINCT * FROM vehicle WHERE VIN='$vin'");
$sql_stock_check = mysql_query("SELECT stock_id FROM vehicle WHERE stock_id='$stockno'");
$VIN_check = mysql_num_rows($sql_VIN_check);
$stock_check = mysql_num_rows($sql_stock_check);
if(($VIN_check > 0) || ($stock_check > 0)){
echo "<tr width='400'><td><div class=\"field\">Please fix the following errors:</div></td></tr><br />";
if($VIN_check > 0){
echo "<tr width='400'><td><div class=\"field\">The VIN sepcified already exists! Please enter Different VIN!</div></td></tr><br />";
unset($vin);
}
if($stock_check > 0){
echo "<tr width='400'><td><div class=\"field\">The stock number sepcified already exists! Please specify a different one!</div></td></<tr><br />";
unset($stockno);
}
include 'reg_inventory.php'; // Show the form again!
exit(); // exit the script so that we do not create this account!
}
$sql = mysql_query("INSERT INTO vehicle (ID, model, make,
year, mileage, color, price, VIN, stock_id, url, cat_id, entered_date)
VALUES(NULL, '$model', '$make', '$year', '$mileage',
'$vcolor', '$price', '$vin', '$stockno', '$url', '$cats', now())")
or die (mysql_error());
if (!$sql) {
echo "<tr width='400'><td>There has been an error creating Vehicle Inventory. Please contact the webmaster.</td></tr>";
} else {
echo("<tr width='400'><td><div class=\"field\">Inventory Information Successful</div></td></tr><br />");
$id = mysql_insert_id();
}
$jid = mysql_insert_id();
if ($cats == '') $cats = array();
$numCats = 0;
foreach ($cats as $cat_ID) {
$sql = "INSERT IGNORE INTO lists SET veID=$jid, catID=$cat_ID";
$ok = @mysql_query($sql);
if ($ok) {
$numCats = $numCats + 1;
} else {
echo("<p>Error inserting Vehicle into category $cat_ID: " .
mysql_error() . '</p>');
}
}
echo("<p><tr width='400'><td><div class=\"field\">" .
"Update was made to <?=$numCats?> categories.</td></tr></p>");
echo "<tr width='400'><td><div class=\"field\">";
echo "Vehicle Information was successful... <br />";
echo "Please wait, you are being redirected to the home page" .
"(If your browser doesn't support this, <a href=\"index.php\">Click here</a>)";
echo "</div></td></tr>";
echo "</table></center>";
?>
</body>
</center>
</html>
you can see the sample here...
http://www.scofieldassociates.com/P...g_inventory.php
it work fine but in the database... the cat_id is 0...
|
haryoh
|
|

August 3rd, 2006, 12:46 PM
|
|
Registered User
|
|
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
it's not inseritng my lookupID to match my vehicleID...
so i changed it to this but it still gives me error messages:
PHP Code:
[s]$jid = mysql_insert_id();[/s]
[s][s]if ($cats != "") {[/s][/s]
[s][s]$cat = "";[/s][/s]
[s][s]foreach ($cats as $key => $cat_ID) {[/s][/s]
[s][s][s][s][s][s][s][s]$cat[] = "('$jid', '$cat_ID')";[/s][/s][/s][/s][/s][/s][/s][/s]
[s][s][s]}[/s][/s][/s]
$values = implode(', ', $cat);
[s][s][s][s]$sql = "INSERT IGNORE INTO lists (veID, catID)" .[/s][/s][/s][/s]
[s][s][s][s][s][s][s][s][s][s][s][s][s]"VALUES ($values)";[/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s]
[s][s][s][s]$ok = @mysql_query($sql);[/s][/s][/s][/s]
[s][s][s][s]if ($ok) {[/s][/s][/s][/s]
[s][s][s][s][s][s][s][s]echo("<p><tr width='400'><td><div class=\"field\">" .[/s][/s][/s][/s][/s][/s][/s][/s]
[s][s][s][s][s][s][s][s][s][s][s][s][s]"Update was made to $cats categories.</td></tr></p>");[/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s]
[s][s][s][s]} else {[/s][/s][/s][/s]
[s][s][s][s][s][s]echo("<p>Error inserting Vehicle into category $cat_ID: " .[/s][/s][/s][/s][/s][/s]
[s][s][s][s][s][s][s][s][s][s][s]mysql_error() . "</p>");[/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s]
[s][s][s][s]}[/s][/s][/s][/s]
[s][s]}[/s][/s]
haryoh
|
|
 |