gpt4 book ai didi

php - 从 textfield 和 textarea 插入到 sql 不起作用

转载 作者:行者123 更新时间:2023-11-29 03:05:27 24 4
gpt4 key购买 nike

请帮忙,将我的文本字段中的数据与我的文本区域结合到数据库中,但无法正常工作,

请帮助了解如何正确组合我的 VALUES。我似乎无法弄清楚如何将我的文本区域的内爆与文本字段中的其他值结合起来。

下面是我的 PHP 代码:

$Category2 = $_POST['Category2'];
$Category3 = $_POST['Category3'];
$Status = $_POST['Status'];
$Date = $_POST['Date'];
$Severity = $_POST['Severity'];
$BanType = $_POST['BanType'];
$XiD = $_POST['XiD'];
$Ticket = $_POST['Ticket'];

//Process the input textareas into arrays
$PhoneNumber = array_map('mysql_real_escape_string', explode("\n", $_POST['PhoneNumber']));
$Createdate = array_map('mysql_real_escape_string', explode("\n", $_POST['Createdate']));
$RemedyTicketNo = array_map('mysql_real_escape_string', explode("\n", $_POST['PhoneNumber']));

//Determine the values with the least amoutn of elements
$min_count = min($PhoneNumber, $Createdate, $RemedyTicketNo);

//Create array to hold INSERT values
$values = array();

//Create the INSERT values
for($index=0; $index<$min_count; $index++)
{
$values[] = "('$RemedyTicketNo[$index]','$PhoneNumber[$index]','$Createdate[$index]',
'$Category2','$Category3','$Status','$Date','$Severity','$BanType','$XiD')";
}

$sql=mysql_query("INSERT into tbl_main
(ars_no,phone_number,create_date,category_1,category_2,status,resolved_date,trouble_type_priority,ban_type,employee_id_name)
VALUES " . implode (',',$values));
if (!$sql) {
die('Invalid query: ' . mysql_error());
}
header("Location: smp_backend_test.php");
}

这是我的 html 代码:它有文本字段,

<td colspan="2"><form id="form1" name="form1" method="post" action="" onsubmit="return validateDBLoad()">
<table width="435" border="0">
<tr>
<td width="115"><div align="left" style="font-size:12px"></div></td>
<td width="167" align="left">&nbsp;</td>
<td width="139" align="right"><input type="reset" name="Clear" id="Clear" value="Clear" /></td>
</tr>
<tr>
<td height="24" style="font-size:12px"><strong>Ticket Source:</strong></td>
<td colspan="2"><select name="Ticket" id="Ticket">
<option value="tickettracker.php">Please select...</option>
<option value="wireless_new.php">Wireless - Remedy</option>
<option value="smp_backend_test.php" selected="selected">SMP - Backend</option>
</select>
<input type="submit" value="Go" id="submit"/></td>
</tr>
<tr>
<td height="24" style="font-size:12px"><strong>Date:</strong></td>
<td colspan="2">
<label for="datepickerID"></label>
<input type="text" name="Date" id="Date" /></td>

</tr>
<tr>
<td style="font-size:12px"><strong>XiD, Name:</strong></td>
<td colspan="2"><span style="font-size:12px">
<select name="XiD" id="XiD">
<option value="Blank" selected="selected">Please Select...</option>
<option value="AAA">AAA</option>
<option value="BBB">BBB</option>
<option value="CCC">CCC</option>
</select>
</span></td>
</tr>
<tr>
<td style="font-size:12px"><strong>Category 2:</strong></td>
<td colspan="2"><input type="text" name="Category" id="Category" value="SMP_Backend" disabled="disabled" /> <input type="hidden" name="Category2" id="Category2" value="SMP_Backend" />
</td>
</tr>
<tr>
<td style="font-size:12px"><strong>Category 3:</strong></td>
<td colspan="2"><label for="Category3"></label>
<select name="Category3" id="Category3">
<option value="Blank" selected="selected">Please Select...</option>
<option value="Pending Request">Pending Request</option>
<option value="Validation Error | Aging">Validation Error|Aging</option>
<option value="Validation Error | RCM">Validation Error|RCM</option>
<option value="Validation Error | Lost Stolen">Validation Error|Lost Stolen</option>
</select></td>
</tr>
<tr>
<td style="font-size:12px"><strong>Status:</strong></td>
<td colspan="2"><input name="Status1" type="text" id="Status1" value="Resolved" disabled="disabled" />
<input name="Status" type="hidden" id="Status" value="Resolved" />
</td>
</tr>
<tr>
<td style="font-size:12px"><strong>Severity:</strong></td>
<td colspan="2"><input type="text" name="SeverityX" id="SeverityX" value="5" size="5" disabled="disabled"/>
<input type="hidden" name="Severity" id="Severity" value="5" size="5" />
</td>
</tr>
<tr>
<td style="font-size:12px"><strong>Ban Type:</strong></td>
<td colspan="2"><span style="font-size:12px">
<input type="text" name="BanType2" id="BanType2" value="I" size="5" disabled="disabled"/>
<input type="hidden" name="BanType" id="BanType" value="I" size="5" />
</span></td>
</tr>
<tr>
<td style="font-size:12px"><strong>Phone No.:</strong></td>
<td style="font-size:12px"><strong>Create Date:</strong></td>
<td style="display:none" >RemedyTicketNo</td>
</tr>

//这是我的文本区域:

            <tr>
<td style="font-size:12px"><textarea name="PhoneNumber" id="PhoneNumber" cols="15" rows="5"></textarea></td>
<td><textarea name="Createdate" id="Createdate" cols="25" rows="5"></textarea></td>
<td style="display:none"><textarea type=hidden name="RemedyTicketNo" id="RemedyTicketNo" cols="15" rows="5"></textarea>
</td>
</tr>
<tr>
<td style="font-size:12px">&nbsp;</td>
<td colspan="2"><span style="font-size:12px">
<input name="DBLoad" type="submit" id="DBLoad" value="DB Load" />
</span></td>
</tr>
</table>
</form></td>

最佳答案

尝试这个我编辑了你的代码

<?php 
if(isset($_POST['DBLoad']))
{
$con=mysql_connect('localhost','root','');
$dbs=mysql_select_db('test',$con);

//print_r($_POST);
//die();
$Category2 = $_POST['Category2'];
$Category3 = $_POST['Category3'];
$Status = $_POST['Status'];
$Date = $_POST['Date'];
$Severity = $_POST['Severity'];
$BanType = $_POST['BanType'];
$XiD = $_POST['XiD'];
$Ticket = $_POST['Ticket'];

//Process the input textareas into arrays
$PhoneNumber = array_map('mysql_real_escape_string', explode("\r\n", $_POST['PhoneNumber']));
$Createdate = array_map('mysql_real_escape_string', explode("\r\n", $_POST['Createdate']));
$RemedyTicketNo = array_map('mysql_real_escape_string', explode("\r\n", $_POST['PhoneNumber']));
//Determine the values with the least amoutn of elements
$min_count = min(count($PhoneNumber), count($Createdate), count($RemedyTicketNo));

//Create array to hold INSERT values
$values = array();

//Create the INSERT values
for($index=0; $index<$min_count; $index++)
{
$values[$index] = "('{$PhoneNumber[$index]}', '{$Createdate[$index]}', '{$RemedyTicketNo[$index]}')";
}

if (isset($RemedyTicketNo))
{
$sql="INSERT into tbl_main (phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name)
VALUES (\"" . implode (', ', $values) ."\", '".$Category2."', '".$Category3."', '".$Status."', '".$Date."','".$Date."','".$Severity."', '".$BanType."', '".$XiD."') ";
$result=mysql_query($sql);

header("Location: smp_backend_test.php");
}
}else
{
?>
<td colspan="2"><form id="form1" name="form1" method="post" action="" >
<table width="435" border="0">
<tr>
<td width="115"><div align="left" style="font-size:12px"></div></td>
<td width="167" align="left">&nbsp;</td>
<td width="139" align="right"><input type="reset" name="Clear" id="Clear" value="Clear" /></td>
</tr>
<tr>
<td height="24" style="font-size:12px"><strong>Ticket Source:</strong></td>
<td colspan="2"><select name="Ticket" id="Ticket">
<option value="tickettracker.php">Please select...</option>
<option value="wireless_new.php">Wireless - Remedy</option>
<option value="smp_backend_test.php" selected="selected">SMP - Backend</option>
</select>
<input type="submit" value="Go" id="submit"/></td>
</tr>
<tr>
<td height="24" style="font-size:12px"><strong>Date:</strong></td>
<td colspan="2">
<label for="datepickerID"></label>
<input type="text" name="Date" id="Date" /></td>

</tr>
<tr>
<td style="font-size:12px"><strong>XiD, Name:</strong></td>
<td colspan="2"><span style="font-size:12px">
<select name="XiD" id="XiD">
<option value="Blank" selected="selected">Please Select...</option>
<option value="AAA">AAA</option>
<option value="BBB">BBB</option>
<option value="CCC">CCC</option>
</select>
</span></td>
</tr>
<tr>
<td style="font-size:12px"><strong>Category 2:</strong></td>
<td colspan="2"><input type="text" name="Category" id="Category" value="SMP_Backend" disabled="disabled" /> <input type="hidden" name="Category2" id="Category2" value="SMP_Backend" />
</td>
</tr>
<tr>
<td style="font-size:12px"><strong>Category 3:</strong></td>
<td colspan="2"><label for="Category3"></label>
<select name="Category3" id="Category3">
<option value="Blank" selected="selected">Please Select...</option>
<option value="Pending Request">Pending Request</option>
<option value="Validation Error | Aging">Validation Error|Aging</option>
<option value="Validation Error | RCM">Validation Error|RCM</option>
<option value="Validation Error | Lost Stolen">Validation Error|Lost Stolen</option>
</select></td>
</tr>
<tr>
<td style="font-size:12px"><strong>Status:</strong></td>
<td colspan="2"><input name="Status1" type="text" id="Status1" value="Resolved" disabled="disabled" />
<input name="Status" type="hidden" id="Status" value="Resolved" />
</td>
</tr>
<tr>
<td style="font-size:12px"><strong>Severity:</strong></td>
<td colspan="2"><input type="text" name="SeverityX" id="SeverityX" value="5" size="5" disabled="disabled"/>
<input type="hidden" name="Severity" id="Severity" value="5" size="5" />
</td>
</tr>
<tr>
<td style="font-size:12px"><strong>Ban Type:</strong></td>
<td colspan="2"><span style="font-size:12px">
<input type="text" name="BanType2" id="BanType2" value="I" size="5" disabled="disabled"/>
<input type="hidden" name="BanType" id="BanType" value="I" size="5" />
</span></td>
</tr>
<tr>
<td style="font-size:12px"><strong>Phone No.:</strong></td>
<td style="font-size:12px"><strong>Create Date:</strong></td>
<td style="display:none" >RemedyTicketNo</td>
</tr>
<tr>
<td style="font-size:12px"><textarea name="PhoneNumber" id="PhoneNumber" cols="15" rows="5"></textarea></td>
<td><textarea name="Createdate" id="Createdate" cols="25" rows="5"></textarea></td>
<td style="display:none"><textarea type=hidden name="RemedyTicketNo" id="RemedyTicketNo" cols="15" rows="5"></textarea>
</td>
</tr>
<tr>
<td style="font-size:12px">&nbsp;</td>
<td colspan="2"><span style="font-size:12px">
<input name="DBLoad" type="submit" id="DBLoad" value="DB Load" />
</span></td>
</tr>
</table>
</form></td>



<?php }?>

为了简单地检查 null 的验证,请遵循代码,您应该检查更多它只是一个示例,您可以如何使用 javascript 来完成您也可以使用 Jquery 来完成

 function validateDBLoad () {
var xid=document.getElementById('XiD').value;
var PhoneNumber=document.getElementById('PhoneNumber').value;
var Status=document.getElementById('Status').value;
var Category2=document.getElementById('Category2').value;
var Category3=document.getElementById('Category3').value;
var Createdate=document.getElementById('Createdate').value;
var Severity=document.getElementById('Severity').value;
var BanType=document.getElementById('BanType').value;


if((xid=="Blank") || (PhoneNumber=="") || (Category2) || (Category3=="Blank") || (Status=="") || (Createdate=="") || (Severity=="") || (document.form1.BanType.value=="")) { alert('Please fill up all the fields..'); return false; } else { alert('Ticket upload Successful.'); return true; } }

然后按照下面的方式制作你的表格

CREATE TABLE IF NOT EXISTS `tbl_main` (
`ars_no` int(12) NOT NULL AUTO_INCREMENT,
`phone_number` varchar(50) NOT NULL,
`category_1` varchar(50) NOT NULL,
`category_2` varchar(50) NOT NULL,
`status` varchar(50) NOT NULL,
`create_date` varchar(50) NOT NULL,
`trouble_type_priority` varchar(50) NOT NULL,
`ban_type` varchar(50) NOT NULL,
`resolved_date` date NOT NULL,
`employee_id_name` varchar(50) NOT NULL,
PRIMARY KEY (`ars_no`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

关于php - 从 textfield 和 textarea 插入到 sql 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16260195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com