douaoli5328 2017-06-10 22:51
浏览 43
已采纳

检查文本框是否有值

I am using a form to insert data to my database. I have a textbox with the ID and NAME name1.

If the user clicks on submit the PHP script sends the data to my database. But the insert statement is also executed when the textbox is empty. If there is no data entered the script creates an empty row in the database.

In my PHP script I want to check if the (value) in the textbox is empty or not.

I have tried multiple codes but the script still sends the empty data to my database.

Does someone know how I can check if there is data entered in the textbox.

Here is my script:

$query1 = "INSERT INTO product(user_id, name, price, tax) VALUES (?, ?, ?, ?)";
$stmt = $db->prepare($query1);
$exec1 = $stmt->execute(array($_SESSION['USER_ID'], $name1, $price1, $tax1));

if($exec1){

echo '<p>product1 is created</p>';  

} else {
echo '<p>error</p>';
}
  • 写回答

2条回答 默认 最新

  • doulai6469 2017-06-10 22:58
    关注

    It looks like that you are not checking if the text field is empty. Please try this:

    if(!empty($_POST['name1'])) {
        $query1 = "INSERT INTO product(user_id, name, price, tax) VALUES (?, ?, ?, ?)";
        $stmt = $db->prepare($query1);
        $exec1 = $stmt->execute(array($_SESSION['USER_ID'], $name1, $price1, $tax1));
    
        if($exec1){
    
        echo '<p>product1 is created</p>';  
    
        } else {
        echo '<p>error</p>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计