Php不插入/发布空值,但仍然插入/填充填充值(Php doesn't insert/post empty value, but still insert/post filled value

系统教程 行业动态 更新时间:2024-06-14 16:59:46
Php不插入/发布空值,但仍然插入/填充填充值(Php doesn't insert/post empty value, but still insert/post filled value)

在询问之前,这是我的表格图片:

正如你所看到的,我有1个表格,有2个表格,每个表格中有6个输入。

“Table Alat”和“Table Bahan”中的输入代码如下:

<form method='post' action='p_input.php'> <table> <tr> <td>1.</td> <td><input type='text' name='nama[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='merk[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='kemasan[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='mhs[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='jml[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='hps[]' style='width:100%;' autocomplete='off'> <input type='hidden' name='jenis[]' value='alat' autocomplete='off'></td> </tr> </table> <table> <tr> <td>1.</td> <td><input type='text' name='nama[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='merk[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='kemasan[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='mhs[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='jml[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='hps[]' style='width:100%;' autocomplete='off'> <input type='hidden' name='jenis[]' value='bahan' autocomplete='off'></td> </tr> <table width='100%' align='center' style='margin-top:0;'> <tr> <td><input name='reset' type='reset' id='bersihkan' value='Reset'> <input name='submit' type='submit' id='ajukan' value='Send'></td> </tr> </table> </table> </form>

这里的区别是<input type='hidden' name='jenis[]' value='alat' autocomplete='off'> ,对于“Table Alat”,值为'alat'为“Table Bahan”的值是'bahan'(我在这里没有麻烦,只是告诉你)。

这是我的p_input.php脚本:

<?php error_reporting(0); session_start(); if(isset($_REQUEST['submit'])) { include "../conf/koneksi.php"; $count = count($_POST['nama']); $jurusan = $_POST['jurusan']; $lab = $_POST['lab']; $materi = $_POST['materi']; $mahasiswa= $_POST['mahasiswa']; for($i = 0; $i < $count; $i++){ $nama = mysql_real_escape_string($_POST['nama'][$i]); $merk = mysql_real_escape_string($_POST['merk'][$i]); $kemasan = mysql_real_escape_string($_POST['kemasan'][$i]); $mhs = mysql_real_escape_string($_POST['mhs'][$i]); $jml = mysql_real_escape_string($_POST['jml'][$i]); $hps = mysql_real_escape_string($_POST['hps'][$i]); $jenis = mysql_real_escape_string($_POST['jenis'][$i]); $sql2= "ALTER TABLE tb_usulan AUTO_INCREMENT = 1"; mysql_query($sql2); $sql=mysql_query("INSERT INTO tb_usulan (nama,merk,kemasan,kebutuhan,jml_kebutuhan,hps,jenis,materi_praktikum) VALUES ('$nama', '$merk', '$kemasan', '$mhs', '$jml', '$hps','$jenis','$materi')") or die(mysql_error()); } } ?>

我的问题是 :如果我只填写“TABLE ALAT”中的输入并将“TABLE BAHAN”中的输入保持为空,那么当我单击“提交”时,我只想在表格中输入以插入/发布到数据库,反之亦然,如果我只填写“TABLE BAHAN”中的输入并将“TABLE ALAT”中的输入保持为空,然后只在“TABLE BAHAN”中输入将插入/发布到数据库。

更新问题 :这只是一个表单和一个提交按钮,并同时将其插入同一个表。

Before asking, here's my form picture:

As you can see, I have 1 form with 2 tables, in each table, there are six inputs.

The input code in "Table Alat" and "Table Bahan" is like this:

<form method='post' action='p_input.php'> <table> <tr> <td>1.</td> <td><input type='text' name='nama[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='merk[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='kemasan[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='mhs[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='jml[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='hps[]' style='width:100%;' autocomplete='off'> <input type='hidden' name='jenis[]' value='alat' autocomplete='off'></td> </tr> </table> <table> <tr> <td>1.</td> <td><input type='text' name='nama[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='merk[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='kemasan[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='mhs[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='jml[]' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='hps[]' style='width:100%;' autocomplete='off'> <input type='hidden' name='jenis[]' value='bahan' autocomplete='off'></td> </tr> <table width='100%' align='center' style='margin-top:0;'> <tr> <td><input name='reset' type='reset' id='bersihkan' value='Reset'> <input name='submit' type='submit' id='ajukan' value='Send'></td> </tr> </table> </table> </form>

The difference in here is <input type='hidden' name='jenis[]' value='alat' autocomplete='off'>, for "Table Alat" the value is 'alat' for "Table Bahan" the value is 'bahan'(I have no trouble in here, just telling you).

This is my p_input.php script:

<?php error_reporting(0); session_start(); if(isset($_REQUEST['submit'])) { include "../conf/koneksi.php"; $count = count($_POST['nama']); $jurusan = $_POST['jurusan']; $lab = $_POST['lab']; $materi = $_POST['materi']; $mahasiswa= $_POST['mahasiswa']; for($i = 0; $i < $count; $i++){ $nama = mysql_real_escape_string($_POST['nama'][$i]); $merk = mysql_real_escape_string($_POST['merk'][$i]); $kemasan = mysql_real_escape_string($_POST['kemasan'][$i]); $mhs = mysql_real_escape_string($_POST['mhs'][$i]); $jml = mysql_real_escape_string($_POST['jml'][$i]); $hps = mysql_real_escape_string($_POST['hps'][$i]); $jenis = mysql_real_escape_string($_POST['jenis'][$i]); $sql2= "ALTER TABLE tb_usulan AUTO_INCREMENT = 1"; mysql_query($sql2); $sql=mysql_query("INSERT INTO tb_usulan (nama,merk,kemasan,kebutuhan,jml_kebutuhan,hps,jenis,materi_praktikum) VALUES ('$nama', '$merk', '$kemasan', '$mhs', '$jml', '$hps','$jenis','$materi')") or die(mysql_error()); } } ?>

And my question is: If I only fill input in "TABLE ALAT" and keep input in "TABLE BAHAN" empty, then when I click submit, I want only input in table alat to insert/post to database, and vice versa, if I only fill input in "TABLE BAHAN" and keep input in "TABLE ALAT" empty, then only input in "TABLE BAHAN" will insert/post to database.

UPDATE QUESTION: This is only one form and one submit button, and insert it to the same table at same time.

最满意答案

记得我添加了输入字段。 所以在应用解决方案之前将其删除。 因为你已经有了原始形式的输入场。

<table> <tr> <td>1.</td> <td><input type='text' name='nama1' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='merk1' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='kemasan1' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='mhs1' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='jml1' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='hps1' style='width:100%;' autocomplete='off'> <input type='hidden' name='jenis1' value='alat' autocomplete='off'></td> </tr> </table> <table> <tr> <td>1.</td> <td><input type='text' name='nama2' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='merk2' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='kemasan2' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='mhs2' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='jml2' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='hps2' style='width:100%;' autocomplete='off'> <input type='hidden' name='jenis2' value='bahan' autocomplete='off'></td> </tr> </table> <input type='submit' name="submit"> </form> </body>

比运行查询的位置只更改该部分。

if(!empty($_POST['name1'])) { //Run First query } if(!empty($_POST['name2'])) { //run your second query }

此代码将执行此操作将检查值。 如果值不为空,那么它将执行查询,否则它将不执行查询

第二个是当你使用代码时你没有任何数组,所以你需要删除$ count变量。 和你的foreach从代码循环。

第三件事。 你需要设置之后的值

if(isset($ _ REQUEST ['submit'])){include“../conf/koneksi.php”;

对于每个像下面这样的场地,因为我很少

$ NAME1 = $ _邮政[ 'NAME1']; $ merk1 = $ _交[ 'merk1'];

对两个表的每个值执行此操作。

您需要更改查询中的值部分,比如您的值将是$ name1,依此类推第一个查询和第二个查询$ name2,依此类推。

希望我按照步骤解释这一点。 并在执行步骤之前。 如果你不明白,在你开始应用之前问我。

谢谢

Remember i added input field. so delete it before you apply the solution. as you already have input feild in your original form.

<table> <tr> <td>1.</td> <td><input type='text' name='nama1' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='merk1' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='kemasan1' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='mhs1' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='jml1' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='hps1' style='width:100%;' autocomplete='off'> <input type='hidden' name='jenis1' value='alat' autocomplete='off'></td> </tr> </table> <table> <tr> <td>1.</td> <td><input type='text' name='nama2' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='merk2' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='kemasan2' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='mhs2' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='jml2' style='width:100%;' autocomplete='off'></td> <td><input type='text' name='hps2' style='width:100%;' autocomplete='off'> <input type='hidden' name='jenis2' value='bahan' autocomplete='off'></td> </tr> </table> <input type='submit' name="submit"> </form> </body>

Than where you are running the query only change that part.

if(!empty($_POST['name1'])) { //Run First query } if(!empty($_POST['name2'])) { //run your second query }

What this code will do it will check for the value. If the values are not empty than it will execute the query other wise it will not execute the query

and second is when you use the code you wont have any array so you need to remove the $count variable. and your foreach loop from the code.

And Third thing. you need to set the value after

if(isset($_REQUEST['submit'])) { include "../conf/koneksi.php";

For each and every feild like below as i am setting few

$name1=$_Post['name1']; $merk1=$_post['merk1'];

Do this for each and every value for both table.

And forth thing you need to change the value part in your query like your values going to be $name1, and so on for first query and for second query $name2 and so on.

Hope i explain this follow the step. and before following the steps. If you dont understand it ask me before you start applying them.

Thanks

更多推荐

本文发布于:2023-04-17 08:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/200089ff932a3ffe8cf5b1f9a6d38c05.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:充值   但仍然   Php   insert   filled

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!