gpt4 book ai didi

php - 创建一个foreach循环以在php变量中缓存html表单名称

转载 作者:行者123 更新时间:2023-11-29 12:13:13 26 4
gpt4 key购买 nike

请帮助我创建一个foreach循环,该循环采用数组的内容,该数组包括html表单中的每个name属性,并将其缓存为php变量,可用于将表单中的数据提交到mySQL数据库中。

我在下面的php代码中包含了我的foreach实例(多次尝试失败)。如果排除了我的foreach循环并且未注释php中注释掉的变量赋值,则该窗体工作正常。

这是HTML表单,首先:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Fixit-log</title>
</head>
<body>
<form action="form_script.php" method="POST">
<section id="identification"
<p>Case: <input type="text" name="case_id" size="7"/></p>
<p>Date: <input type="text" name="day" size="15" /</p>
<p>Owner First Name: <input type="text" name="owner_first_name" size="15" /> </p>
<p>Owner Last Name: <input type="text" name="owner_last_name" size="20" /</p>
<p>Phone: <input type="text" name="phone" size= "10"/></p>
<p>Email: <input type="email" name="email" size= "30"/><p>
<p>Address: <input type="text" name="address" size= "50"/</p>
<p>City: <input type="text" name="city" size="30"/</p>
<p>State: <input type="text" name= "state" size="2"</p>
<p>ZIP: <input type="text" name="zip" size="5"</p>
</section>

<section id="hardware">
<p>OS Make: <input type="radio" name="os_make" value="windows"/>Windows
<input type="radio" name="os_make" value="mac"/> Mac
<input type="radio" name="os_make" value="linux"/> Linux
<input type="radio" name="os_make" value="none"/>None</p>
<p>OS Type: <input type="text" name="os_type" size="15"/</p>
<p>OS Architecture <input type="radio" name="os_architecture" value="32"/> 32
<input type="radio" name="os_architecture" value="64"/>64
<input type="radio" name="os_architecture" value="other"/>Other</p>
<p>S/N: <input type="text" name="sn" size="20"</p>
<p>RAM: <input type="text" name="ram_amount" size="10"</p>
<p>CPU Make: <input type="radio" name="cpu_make" value="intel"/>Intel
<input type="radio" name="cpu_make" value="amd"/>AMD</p>
<input type="radio" name="cpu_make" value="other">Other</p>
<p>CPU Model: <input type="text" name="cpu_model" size="0"/></p>
<p>CPU Architecture: <input type="radio" name="cpu_architecture" value="32"/>32
<input type="radio" name="cpu_architecture" value="64"/>64
<input type="radio" name="cpu_architecture" value="other"/>Other</p>
<p>Other equipment: <input type="text" name="equipment_other" size="50"</p>
</section>

<section id="history">
<p>C/O: <input type="text" name="co" size="255"</p>
<p>Began: <input type="text" name="began" size="30"</p>
<p>Previous Work: <input type="radio" name="previous_work" value="yes"/>Yes
<input type="radio" name="previous_work" value="no"/> No
<input type="radio" name="previous_work" value="unknown"/> Unknown</p>
<p>Who?<input type="text" name="previous_who" size="20"/></p>
<p>When?<input type="text" name="previous_when" size="20"/></p>
<p>Comments: <input type="text" name="comments_initial" size="255"/></p>
</section>

<section id="diagnostic">
<p>Boot Time (sec): <input type="text" name="boot_time_initial" size="4"/</p>
<p>Memory: <input type="text" name="memory_use_initial" size="3" /</p>
<p>CPU: <input type="text" name="cpu_use_initial" size="3"/</p>
<p>Evaluation <input type="text" name="evaluation" size="254"</p>
</section>

<section id="plan">
<p>Actions: <input type="text" name="actions" size="254"/</p>
<p>Overview: <input type="text" name="overview" size="254"/</p>
<p>Summary: <input type="text" name="summary" size="254"/</p>
<p>Recommendations: <input type="text" name="recommendations" size="254"/</p>
</section>

<input type="submit" name="submit" value="Submit">
<input type="reset" name="reset" value="Reset">
</form>
</body>
</html>


这是PHP脚本:

<?php
define('DB_NAME', 'x');
define('DB_USER', 'x');
define('DB_PASSWORD', 'x');
define('DB_HOST', 'x');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

/*echo 'connected succesfully';*/

$formItems = array('case_id','day','owner_first_name','owner_last_name','phone','email','address','city','state','zip','os_make','os_type','os_architecture','sn','ram_amount','cpu_make','cpu_model','cpu_architecture','equipment_other','co','began','previous_work','previous_who','previous_when','comments_initial','boot_time_initial','memory_use_initial','evaluation','overview','actions','summary','recommendations');

foreach ($formItems as $formItem){
print '$' . $formItem . '=' . '$_POST' . '[' . '\'' . $formItem . '\'' . '];' . ' ';
}
//Loop replaces list below

/*$case_id=$_POST['case_id'];
$day=$_POST['day'];
$owner_first_name=$_POST['owner_first_name'];
$owner_last_name=$_POST['owner_last_name'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$address=$_POST['address'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$os_make=$_POST['os_make'];
$os_type=$_POST['os_type'];
$os_architecture=$_POST['os_architecture'];
$sn=$_POST['sn'];
$ram_amount=$_POST['ram_amount'];
$cpu_make=$_POST['cpu_make'];
$cpu_model=$_POST['cpu_model'];
$cpu_architecture=$_POST['cpu_architecture'];
$equipment_other=$_POST['equipment_other'];
$co=$_POST['co'];
$began=$_POST['began'];
$previous_work=$_POST['previous_work'];
$previous_who=$_POST['previous_who'];
$previous_when=$_POST['previous_when'];
$comments_initial=$_POST['comments_initial'];
$boot_time_initial=$_POST['boot_time_initial'];
$memory_use_initial=$_POST['memory_use_initial'];
$cpu_use_initial=$_POST['cpu_use_initial'];
$evaluation=$_POST['evaluation'];
$overview=$_POST['overview'];
$actions=$_POST['actions'];
$summary=$_POST['summary'];
$recommendations=$_POST['recommendations'];
*/

$sql = "INSERT INTO jobs (case_id, day, owner_first_name, owner_last_name, phone, address, city, state, zip, email, os_make, os_type, os_architecture, sn, ram_amount, cpu_make, cpu_model, cpu_architecture, equipment_other, co, began, previous_work, previous_who, previous_when, comments_initial, boot_time_initial, memory_use_initial, cpu_use_initial, evaluation, overview, actions, summary, recommendations) " . " VALUES ('$case_id', '$day', '$owner_first_name', '$owner_last_name', '$phone', '$address', '$city', '$state', '$zip', '$email', '$os_make', '$os_type', '$os_architecture', '$sn', '$ram_amount', '$cpu_make', '$cpu_model', '$cpu_architecture', '$equipment_other', '$co', '$began', '$previous_work', '$previous_who', '$previous_when', '$comments_initial', '$boot_time_initial', '$memory_use_initial', '$cpu_use_initial', '$evaluation', '$overview', '$actions', '$summary', '$recommendations')";

if(!mysql_query($sql)) {
die('Error: ' . mysql_error());
}

mysql_close();
?>


而且,这是我收到的错误信息:

$ case_id = $ _ POST ['case_id']; $ day = $ _ POST ['day']; $ owner_first_name = $ _ POST ['owner_first_name']; $ owner_last_name = $ _ POST ['owner_last_name']; $ phone = $ _ POST ['phone']; $ email = $ _ POST ['email']; $ address = $ _ POST ['address']; $ city = $ _ POST ['city']; $ state = $ _ POST ['state']; $ zip = $ _ POST ['zip']; $ os_make = $ _ POST ['os_make']; $ os_type = $ _ POST ['os_type']; $ os_architecture = $ _ POST ['os_architecture']; $ sn = $ _ POST ['sn']; $ ram_amount = $ _ POST ['ram_amount']; $ cpu_make = $ _ POST ['cpu_make']; $ cpu_model = $ _ POST ['cpu_model']; $ cpu_architecture = $ _ POST ['cpu_architecture']; $ equipment_other = $ _ POST ['equipment_other']; $ co = $ _ POST ['co']; $ began = $ _ POST ['began']; $ previous_work = $ _ POST ['previous_work']; $ previous_who = $ _ POST ['previous_who']; $ previous_when = $ _ POST ['previous_when']; $ comments_initial = $ _ POST ['comments_initial']; $ boot_time_initial = $ _ POST ['boot_time_initial']; $ memory_use_initial = $ _ POST ['memory_use_initial']; $ evaluation = $ _ POST ['evaluation']; $ overview = $ _ POST ['overview']; $ actions = $ _ POST ['actions']; $ summary = $ _ POST ['summary']; $ recommendations = $ _ POST ['recommendations'];
注意:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中未定义的变量:case_id

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的day

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的owner_first_name

注意:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中未定义的变量:owner_last_name

注意:未定义的变量:第63行上的/opt/lampp/htdocs/fixit-log/form_script.php中的电话

注意:未定义的变量:第63行上的/opt/lampp/htdocs/fixit-log/form_script.php中的地址

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的城市

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的状态

注意:未定义的变量:在第63行的/opt/lampp/htdocs/fixit-log/form_script.php中压缩

注意:未定义的变量:第63行上的/opt/lampp/htdocs/fixit-log/form_script.php中的电子邮件

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的os_make

注意:未定义的变量:/opt/lampp/htdocs/fixit-log/form_script.php在第63行中的os_type

注意:未定义的变量:/opt/lampp/htdocs/fixit-log/form_script.php在第63行中的os_architecture

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的sn

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的ram_amount

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的cpu_make

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的cpu_model

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的cpu_architecture

注意:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中未定义的变量:equipment_other

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的co

注意:未定义的变量:从/opt/lampp/htdocs/fixit-log/form_script.php开始于第63行

注意:未定义的变量:/opt/lampp/htdocs/fixit-log/form_script.php在第63行中的previous_work

注意:第63行上的/opt/lampp/htdocs/fixit-log/form_script.php中未定义的变量:previous_who

注意:未定义的变量:63行/opt/lampp/htdocs/fixit-log/form_script.php中的previous_when

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的comments_initial

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的boot_time_initial

注意:第63行上的/opt/lampp/htdocs/fixit-log/form_script.php中的未定义变量:memory_use_initial

注意:未定义的变量:第63行的/opt/lampp/htdocs/fixit-log/form_script.php中的cpu_use_initial

注意:未定义的变量:在第63行的/opt/lampp/htdocs/fixit-log/form_script.php中进行评估

注意:未定义的变量:/opt/lampp/htdocs/fixit-log/form_script.php中的概述,位于第63行

注意:未定义的变量:/opt/lampp/htdocs/fixit-log/form_script.php中第63行上的操作

注意:未定义的变量:/opt/lampp/htdocs/fixit-log/form_script.php中的摘要在第63行

注意:未定义的变量:第63行上的/opt/lampp/htdocs/fixit-log/form_script.php中的建议
错误:键“ case_id”的条目“ 0”重复

最佳答案

看来您正在尝试使用名称来自$formItems数组的变量来创建变量。我认为您可以为此使用variable variables。按照问题显示的方式进行操作:

foreach ($formItems as $formItem){
print '$' . $formItem . '=' . '$_POST' . '[' . '\'' . $formItem . '\'' . '];' . ' ';
}


只会打印出您要完成的工作,而不是实际执行。相反,您可以使用它来获取所需的变量:

foreach ($formItems as $formItem) {
$$formItem = mysql_escape_string($_POST[$formItem]);
}


附带说明,在尝试将这些值插入数据库之前,应先转义这些值。我添加了使用mysql函数转义值的方法,因为您正在代码中使用这些值。但是不建议使用这些功能,您应该研究将代码更改为使用mysqli或PDO。

关于php - 创建一个foreach循环以在php变量中缓存html表单名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30311796/

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