2015-02-04 2 views
0

У меня есть форма с некоторыми полямиPOST пустые поля приведет к ошибке запроса

<form action="add.php" method="post"> 
/*Some fields*/ 
</form> 

Также я создал следующую функцию цитаты и избежать форме сохраняемых значений

<?php 
    // Quote and escape form submitted values 
    function db_quote($value) { 
     $connection = db_connect();//Connection with database "NO ISSUE HERE" 
     return "'" . mysqli_real_escape_string($connection,$value) . "'";//Maybe the issue here 
    } 
?> 

Затем я передать значения в функционируют следующим образом:

$inventoryId = db_quote($_POST['inventoryId']); 
$sn = db_quote($_POST['sn']); 
$model = db_quote($_POST['model']); 
//etc... 

Каждая вещь работает нормально, если я заполняю все поля, но если есть на lea й один пустой поданной т.е. никакого значения, введенного пользователем, я получаю следующую ошибку

Catchable fatal error: Object of class mysqli could not be converted to string in etc... 

Вот запрос я пытаюсь запустить

<?php 
    $sql = "INSERT INTO inventory (id,manufacturer_id,supplier_id,servicer_id,operator_id,sn,model,inventory_name,inventory_type,description,power,purchase_order,purchase_cost,arrival_date,installation_date,warranty_date,incident_history,conditions,m_next_date,m_start_date,m_deadline,lifetime,inspection_frequency,location,purchased_from) 
    VALUES ($inventoryId,$manufacturer,$supplier,$servicesId,$operatorId,$sn,$model,$inventoryName,$inventorType,$description,$power,$purchaseOrder,$purchaseCost,$arrivalDate,$installationDate,$warranty,$incident,$conditions,$nextDate,$startDate,$deadline,$lifetime,$inspection,$location,$purchasedFrom);"; 

    if ($connection->query($sql) === TRUE) { 
     echo "<p>New inventory ".$inventoryId." created successfully</p>"; 
    } else { 
    echo "Error: " . $sql . "<br>" . $connection; 
    } 
    $connection->close(); 
?> 

Update: Вопрос только с колонками Auto Increment

+0

Я думаю, что вы пропустили '' '' '' $ inventoryId, $ производитель, 'как' '$ inventoryId ',' $ manufacturer ',' – RubahMalam

+0

@RubahMalam db_quote добавить одинарный кавычек' ' –

ответ

0
<?php 
    // Quote and escape form submitted values 
    function db_quote($value) { 
     $connection = db_connect();//Connection with database "NO ISSUE HERE" 
     return "'" . mysqli_real_escape_string($connection,$value) . "'";//Maybe the issue here 
    } 
?> 

Преобразовать его

<?php 
    // Quote and escape form submitted values 
    function db_quote($value) { 
     if(!$value) return ''; 
     $connection = db_connect();//Connection with database "NO ISSUE HERE" 
     return "'" . mysqli_real_escape_string($connection,$value) . "'";//Maybe the issue here 
    } 
?> 
+0

Нет, это не Работа. По-прежнему получить ту же ошибку, если хотя бы одна пустая. Спасибо. –

+0

попробуйте один раз с помощью функции sprintf php при создании вашего sql – shahmanthan9

Смежные вопросы