作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请帮助我创建一个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
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();
?>
最佳答案
看来您正在尝试使用名称来自$formItems
数组的变量来创建变量。我认为您可以为此使用variable variables。按照问题显示的方式进行操作:
foreach ($formItems as $formItem){
print '$' . $formItem . '=' . '$_POST' . '[' . '\'' . $formItem . '\'' . '];' . ' ';
}
foreach ($formItems as $formItem) {
$$formItem = mysql_escape_string($_POST[$formItem]);
}
关于php - 创建一个foreach循环以在php变量中缓存html表单名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30311796/
我是一名优秀的程序员,十分优秀!