我是PHP的新手。我正在处理一个表格,其中包含一个表格,其中包含两个文本框和一个组合框。我使用JavaScript生成表行,同样删除它。 我还引用了一个博客来生成行。现在我被困在如何将生成的文本框中的值保存到MySQL数据库中。谁能帮我吗?这将是一个巨大的帮助。
这是我的代码:
<?php
session_start();
if (!isset($_SESSION["username"])) {
header("Location: unauthorize_access.php");
}
if(isset($_POST['submit']))
{
$length = count($_POST['testparameters']);
for($i=0; $i< $length; $i++){
$TestParameters=$_POST['testparameters'][$i];
if(!empty($TestParameters)){
$InsertQuery = "INSERT INTO color(name) VALUES('$TestParameters')";
$result=mysql_query($InsertQuery) or die(mysql_error());
}
}
if(!$InsertQuery)
{
die('Could not enter data:' . mysql_error());
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Makhtab</title>
<link rel="stylesheet" type="text/css" href="form2/view.css" media="all">
<script type="text/javascript" src="form2/view.js"></script>
<script type="text/javascript" src="form2/calendar.js"></script>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function deleteRow(row)
{
var i=row.parentNode.parentNode.rowIndex;
document.getElementById('POITable').deleteRow(i);
}
function insRow()
{
console.log( 'hi');
var x=document.getElementById('POITable');
var new_row = x.rows[1].cloneNode(true);
var len = x.rows.length;
new_row.cells[0].innerHTML = len;
var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
inp1.id += len;
inp1.value = '';
var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
inp2.id += len;
inp2.value = '';
x.appendChild( new_row );
}
</script>
<script type="text/javascript">
function deletezimRow(row)
{
var i=row.parentNode.parentNode.rowIndex;
document.getElementById('zimtable').deleteRow(i);
}
function inszimRow()
{
console.log( 'hi');
var x=document.getElementById('zimtable');
var new_row = x.rows[1].cloneNode(true);
var len = x.rows.length;
new_row.cells[0].innerHTML = len;
var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
inp1.id += len;
inp1.value = '';
var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
inp2.id += len;
inp2.value = '';
x.appendChild( new_row );
}
</script>
<!--<style type="text/css" title="currentStyle">
@import "tran/css/layout-styles.css";
@import "tran/css/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>-->
<script type="text/javascript" src="tran/js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="tran/js/jquery-ui-1.8.14.custom.min.js"></script>
<script type="text/javascript" src="tran/js/jq-ac-script.js"></script>
</head>
<body id="main_body" >
<img id="top" src="form2/top.png" alt="">
<div id="form_container">
<h1><a>Untitled Form</a></h1>
<form id="makhtab" class="appnitro" enctype="multipart/form-data" method="post" action="">
<div class="form_description">
<h2>Makhtab Details</h2>
<!--<p>This is your form description. Click here to edit.</p>-->
</div>
<table border="0px" width="100%">
<tr>
<td><label class="description" for="element_1">Code</label></td><td><input id="element_1" name="code" class="element text small" type="text" maxlength="6" value=""/></td>
</tr>
<tr>
<td><label class="description" for="element_1">Name</label></td><td><input id="element_1" name="name" class="element text large" type="text" maxlength="40" value=""/> </td>
</tr>
<tr>
<td><label class="description" for="element_1">Address</label></td><td><input id="element_4_1" name="add1" class="element text large" value="" type="text"></td>
</tr>
<tr>
<td></td><td><input id="element_4_1" name="add2" class="element text large" value="" type="text"></td>
</tr>
<tr>
<td></td><td><input id="element_4_1" name="add3" class="element text large" value="" type="text"></td>
</tr>
<tr>
<td><label class="description" for="element_1">City</label></td><td><select name="cmbcity" class="element text medium" style="font-size:18px;"/>
<option value=""></option>
<?php
mysql_connect("localhost", "root", '') or die(mysql_error());
mysql_select_db("webapp") or die(mysql_error());
$result = mysql_query("SELECT `name` from city ") or die(mysql_error());
while ($year = mysql_fetch_array($result)) {
echo "<option value='$year[name]'>$year[name]</option>";
}
?>
</select>
</tr>
<tr>
<td><label class="description" for="element_1">State</label></td><td><select name="cmbstate" class="element text medium" style="font-size:18px;"/>
<option value=""></option>
<?php
mysql_connect("localhost", "root", '') or die(mysql_error());
mysql_select_db("webapp") or die(mysql_error());
$result = mysql_query("SELECT `name` from state ") or die(mysql_error());
while ($year = mysql_fetch_array($result)) {
echo "<option value='$year[name]'>$year[name]</option>";
}
?>
</select>
</tr>
</tr>
<tr>
<td><label class="description" for="element_1">Country</label></td><td><select name="cmbcountry" class="element text medium" style="font-size:18px;"/>
<option value=""></option>
<?php
mysql_connect("localhost", "root", '') or die(mysql_error());
mysql_select_db("webapp") or die(mysql_error());
$result = mysql_query("SELECT `name` from country ") or die(mysql_error());
while ($year = mysql_fetch_array($result)) {
echo "<option value='$year[name]'>$year[name]</option>";
}
?>
</select>
<tr>
<td><label class="description" for="element_1">Telephone</label></td><td><input id="element_1" name="country" class="element text medium" type="text" maxlength="255" value=""/></td>
</tr>
<tr>
<td><label class="description" for="element_1">Mobile</label></td><td><input id="element_1" name="country" class="element text medium" type="text" maxlength="10" value=""/></td>
</tr>
<tr>
<br />
</tr>
</table>
<tr>
<br /><p style="border-bottom: 1px dotted #ccc;"></p><br />
<div class="form_description">
<h2>Zimmedar Details</h2>
<!--<p>This is your form description. Click here to edit.</p>-->
</div>
</tr>
<input type="button" id="addmorezimbutton" value="Add zimmedar" onclick="insRow()"/><br/><br/>
<table id="zimtable" border="0px" size="100px" cellspacing="0" cellpadding="2">
<tr>
<td width="10" align="center"><label class="description">Srno. </label></td>
<td width="118"><label class="description">Zimmedar Name</label></td>
<td width="118"><label class="description">Mobile</label></td>
<td width="10"></td>
<td width="10" align="center"></td>
</tr>
<tr>
<td>1</td>
<td width="210"><input size="40" maxlenght="40" type="text" id="zimame[]"/></td>
<td width="80"><input size="10" maxlenght="10" type="text" id="zimmob[]"/></td>
<!--<td><input size="45" maxlenght="10" type="text" id="momob"/> </td>-->
<td><input type="button" id="addzimmore" value="+" onclick="inszimRow()"/></td>
<td align="center"><input type="button" id="delzimbutton" value="-" onclick="deletezimRow(this)"/></td>
</tr>
</table>
<tr>
<br /><p style="border-bottom: 1px dotted #ccc;"></p><br />
<div class="form_description">
<h2>Muallim Details</h2>
<!--<p>This is your form description. Click here to edit.</p>-->
</div>
</tr>
<input type="button" id="addmorePOIbutton" value="Add Muallims" onclick="insRow()"/><br/><br/>
<table id="POITable" border="0px" size="100px" cellspacing="0" cellpadding="2">
<tr>
<td width="10" align="center"><label class="description">Srno. </label></td>
<td width="118"><label class="description">Moallim Name</label></td>
<td width="118"><label class="description">Mobile</label></td>
<td width="118"><label class="description">Qabiliyat</label></td>
<td width="10"></td>
<td width="10" align="center"></td>
</tr>
<tr>
<td>1</td>
<td width="210"><input size="40" maxlenght="40" type="text" id="moname[]"/></td>
<td width="80"><input size="10" maxlenght="10" type="text" id="momob[]"/></td>
<td><select name="cmbmuallim[]" class="element text large" style="font-size:18px;"/>
<option value=""></option>
<?php
mysql_connect("localhost", "root", '') or die(mysql_error());
mysql_select_db("webapp") or die(mysql_error());
$result = mysql_query("SELECT `name` from qabiliyat ") or die(mysql_error());
while ($year = mysql_fetch_array($result)) {
echo "<option value='$year[name]'>$year[name]</option>";
}
?>
</select></td>
<!--<td><input size="45" maxlenght="10" type="text" id="momob"/> </td>-->
<td><input type="button" id="addmore" value="+" onclick="insRow()"/></td>
<td align="center"><input type="button" id="delPOIbutton" value="-" onclick="deleteRow(this)"/></td>
</tr>
</table>
<br /><p style="border-bottom: 1px dotted #ccc;"></p><br />
<table border="0px" width="85%">
<tbody><tr>
<td width="105"><label class="description">No. of Students</label></td>
<td width="65"><input type="text" name=stu" size="5" maxlength="5"></input></td>
<td width="105"><label class="description">No. of Batches</label></td><td width="14"><input type="text" name="batch" size="5" maxlength="3"></input></td>
</tr>
<tr>
</tr>
</tbody>
</table>
</div>
</div>
</form>
</table>
</form>
</body>
</html>
答案 0 :(得分:-1)
如果我错了,请纠正我。 我没有看到任何打开mysql数据库连接的代码。
见这个例子。
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
} else {
echo "Error creating database: " . mysql_error();
}
?>
在使用 mysql_query 插入记录之前,请确保您打开了如上所述的mysql数据库连接。