不要将空值插入mysql db

时间:2011-04-03 18:27:56

标签: php mysql

当我尝试输入新值或更改值或删除值(无)到产品1或产品2

我不想在数据库中插入NULL值。

产品表

+-------------+--------------+---------------+
| products_id | categories_id| products_name |
+-------------+--------------+---------------+
| 1           | 1            | product 1     |
| 2           | 1            | product 2     |
+-------------+--------------+---------------+

products_extra_fields TABLE

+--------------------------+---------------------------+
| products_extra_fields_id | products_extra_fields_name| 
+--------------------------+---------------------------+
| 1                        | sugar                     |
| 2                        | cocoa                     |
| 3                        | butter                    |
| 4                        | milk                      |
+--------------------------+---------------------------+

products_to_products_extra_fields TABLE

+-------------+---------------------------+-----------------------------+
| products_id | products_extra_fields_id  | products_extra_fields_value |
+-------------+---------------------------+-----------------------------+
| 1           | 1                         | 20%                         |
| 1           | 2                         | 45%                         |
| 1           | 3                         | 27%                         |
| 2           | 1                         | 12%                         |
+-------------+---------------------------+-----------------------------+

phpscript

<?php

require ('mysql/mysql_connect2.php'); // Connect to the database.
if (isset ($_POST['submit'])) {

    $products_id = $_POST['products_id'];

    echo "products_id = ".$products_id;
    echo "<br/><br/>";

    for($i = 0; $i<count($_POST['products_extra_fields_value']); $i++){

        $products_extra_fields_id = $_POST['products_extra_fields_id'][$i];
        $products_extra_fields_value = $_POST['products_extra_fields_value'][$i];

        if($products_extra_fields_value != ''){
        echo "products_extra_fields_id = ".$products_extra_fields_id;
        echo "<br/>";
        echo "products_extra_fields_value = ".$products_extra_fields_value;
        echo "<br/><br/>";
    }else{

    }

$query = "INSERT into products_to_products_extra_fields 
    set 
    products_id = '$products_id', 
    products_extra_fields_id = '$products_extra_fields_id', 
    products_extra_fields_value = '$products_extra_fields_value'

    ON DUPLICATE KEY UPDATE 

    products_extra_fields_value = '$products_extra_fields_value'

    "; 
    $result = mysql_query ($query); // Execute the query.
}
if ($result) {

header("refresh: 2; url='products.php?action=gopro_cat&categories_id={$_POST['products_id']}'");
        }else{
        print "<p>Could update the entry because: <b>" . mysql_error() . "</b><BR/><BR/>The query was: $query</p>";
    }   
mysql_close();

}else{
$products_id = $_GET['products_id'];
$categories_id = $_GET['categories_id'];

    // Check for a valid entry ID in the URL.
    if (is_numeric ($products_id) ) {

// Define the query.
$query = "SELECT   products_extra_fields.products_extra_fields_id, 
products_name,  
products_extra_fields_name,  
products_extra_fields_value FROM 

products, 
products_extra_fields LEFT JOIN products_to_products_extra_fields ON 
products_to_products_extra_fields.products_extra_fields_id = 
products_extra_fields.products_extra_fields_id AND 

products_to_products_extra_fields.products_id='$products_id' 

GROUP BY products_extra_fields.products_extra_fields_id";

if ($result = mysql_query ($query)) {

$query2 = "SELECT products_name FROM products, categories WHERE 
products.products_id='$products_id' AND 
categories.categories_id = '$categories_id'";

if ($result2 = mysql_query ($query2)) {
$row2 = mysql_fetch_array ($result2);
$products_name = $row2['products_name'];
?>

<form action='products_edit2.php' enctype='multipart/form-data' method='POST'>
<table width="50%" border="0" cellpadding="2" cellspacing="2">
<tr>
<td width="20%" align="right">PRODUCTS NAME:</td>
<td><input type="text" name="products_name" size="80" value="<?php echo $products_name; ?>" /></td>
</tr>
<?php 
}
/***** ***** *****/

while ($row = mysql_fetch_array ($result)) {
$products_extra_fields_name = $row['products_extra_fields_name'];
$products_extra_fields_value = $row['products_extra_fields_value'];
$products_extra_fields_id = $row['products_extra_fields_id'];
?>
<tr>
<td width="20%" align="right">
<input type="text" name="products_extra_fields_id[]" size="5" value="<?php echo $products_extra_fields_id; ?>" />

<?php echo $products_extra_fields_id .'-'. $products_extra_fields_name; ?></td>
<td><input type="text" name="products_extra_fields_value[]" size="80" value="<?php echo $products_extra_fields_value; ?>" /></td>
</tr>

<?php }

/***** ***** END *****/
?>
<input type="ÔÅ×Ô" name="products_id" size="10" value="<?php echo $products_id; ?>" />

<tr>
<td width="100%" colspan="2" align="right"><input type="submit" name="submit" value="SAVE"/></td>
</tr>
</table>
</form>

<?php
} else { // Couldn't get the information.
print "<p>Could retrieve the entry because: <b>" . mysql_error() . "</b><br/><br/>. The query was $query.</p>";
}
}else{ // No ID set.
print '<p><b>You must have made a mistake in using this page.</b></p>';
}
}
?>

=============================================== ============================================

for($i = 0; $i<count($_POST['products_extra_fields_value']); $i++){

        $_POST['products_extra_fields_id'][$i];
        $_POST['products_extra_fields_value'][$i];

        if($_POST['products_extra_fields_value'][$i] != ''){
        echo "products_extra_fields_id = ".$products_extra_fields_id = $_POST['products_extra_fields_id'][$i];
        echo "<br/>";
        echo "products_extra_fields_value = ".$products_extra_fields_value = $_POST['products_extra_fields_value'][$i];
        echo "<br/><br/>";

        echo "if".$products_extra_fields_value;
        echo "<br/><br/>";
    }else{
        echo "else".$products_extra_fields_value;
        echo "<br/><br/>";
    }
}

使用上述代码,您只能插入新值(或更改)。

问题是,如果您愿意,则无​​法删除任何值。

有没有解决方案?

2 个答案:

答案 0 :(得分:0)

如果您不想将NULL值插入数据库,请将表模式修改为不允许NULL。您可以在MySQL Query Browser或Workbench中执行此操作。

答案 1 :(得分:0)

我会修改这部分代码来做类似的事情,这要求产品ID至少为1个字符。 trim命令删除任何空格。

$min_product_id_length = 1;

if (isset ($_POST['submit'])) {
    $products_id = trim($_POST['products_id']);
    if (strlen($products_id) < $min_product_id_length) {
        die('the product id must be more than '.$min_product_id_length.' char');
    }
//more stuff goes here...
}