Databases Reference
In-Depth Information
$description=clean($_POST["description"][$gift_id], 255);
$color =clean($_POST["color" ][$gift_id], 30);
$shop =clean($_POST["shop" ][$gift_id], 100);
$price =clean($_POST["price" ][$gift_id], 30);
// If the gift_id is 0, this is a new gift, so set the
// gift_id to be empty; MySQL will automatically assign a
// unique gift_id to the new gift.
if($gift_id==0)
$gift_id='';
// If any of the attributes are empty, don't update the database.
if(
!strlen($quantity ) ||
!strlen($description) ||
!strlen($color ) ||
!strlen($shop ) ||
!strlen($price )
)
{
// If this isn't the blank row for optionally adding a new gift,
// or if it is the blank row and the user has actually typed something in,
// display an error message.
if(!empty($gift_id)
||
strlen(
$quantity.
$description.
$color.
$shop.
$price)
)
echo "<font color='red'>There must be no empty fields - not updating:<br />".
"([$quantity], [$description], [$color], [$shop], [$price]</font><br />";
}
else
{
// Add or update the gifts table
$query = "REPLACE INTO gifts ".
"(gift_id, description,shop,quantity,color,price,username)".
" values ('$gift_id', '$description', '$shop', $quantity,
'$color', '$price', NULL)";
// Run the query through the connection
if (@ mysqli_query($connection, $query)==FALSE)
showerror($connection);
}
}
Notice that in our SQL query, we explicitly list field names before specifying the values.
The order of the values must match the order that the field names are listed in, but
these don't need to match the order of the fields in the database table; if we had omitted
the initial list of field names, the field values would need to be in the same order as the
fields in the table. Note also that we haven't stored the result of the REPLACE INTO query
 
Search WWH ::




Custom Search