Reputation: 294
Im having massive frustrations with this dynamic row add and post to mysql. The variables are wrong Im sure. Also the script works fine. I only need help posting to MYSQL. Please forgive me... Im a beginner.
Thanks in advance!
Script WORKS FINE!
<script type='text/javascript'>
$(document).ready(function() {
var currentItem = 1;
$('#addnew').click(function(){
currentItem++;
$('#items').val(currentItem);
var strToAdd = `<tr><th>Company Name</th><th>Street</th></tr><td><input type="text" id="company'+currentItem+'" name="company'+currentItem+'" ></td><td><input id="street'+currentItem+'" name="street'+currentItem+'" ></td></table><br><table> <tr><th>Contact Name</th><th>Contact Title/Role</th></tr><td><input type="text" id="contact'+currentItem+'" name="contact'+currentItem+'" ></td><td><input type="text" id="title'+currentItem+'" name="title'+currentItem+'" ></td></table><tr><th>DX or Chiller</th><th>Equipment Description</th></tr><tr><select name="typeofunit'+currentItem+'" id="typeofunit'+currentItem+'" ><option value="DX">DX</option><option value="Chiller">Chiller</option></select></td><td><input name="unitdescription'+currentItem+'" id="unitdescription'+currentItem+'"></td></tr>`
$('#data').append(strToAdd);
});
});
</script>
HTML WORKS FINE
<html>
<body>
</head>
<body>
<form action="function.php" method="post">
<table>
<tr><th>Company Name</th><th>Street</th></tr>
<td><input type="text" id="company" name="company" ></td>
<td><input id="street" name="street" ></td>
</table>
<br>
<table>
<tr><th>Contact Name</th><th>Contact Title/Role</th></tr>
<td><input type="text" id="contact" name="contact" ></td>
<td><input type="text" id="title" name="title" ></td>
</table>
<tr><th>DX or Chiller</th><th>Equipment Description</th></tr>
<tr>
<select name="typeofunit" id="typeofunit" >
<option value="DX">DX</option>
<option value="Chiller">Chiller</option>
</select>
</td>
<td>
<input name="unitdescription" id="unitdescription">
</td>
</table>
<p class="login button">
<input type="button" id="addnew" name="addnew" value="Add Another Unit" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
PHP THIS IS THE PROBLEM?
<?php
$con = mysql_connect("localhost","ptgpfaso_root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ptgpfaso_mydatabase", $con);
for( $i = 1; $i <= $count; $i++ )
{
$company = $_POST['company'];
$street = $_POST['street'];
$typeofunit = $_POST['typeofunit'.$i];
$unitdescription = $_POST['unitdescription'.$i];
}
$sql="INSERT INTO equips (company,street,typeofunit,unitdescription)
VALUES
('".$company."','".$street."','".$typeofunit."','".$unitdescription."',)";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Thank you for your submission";
?>
Upvotes: 0
Views: 49
Reputation: 44844
Assuming you are getting correct data in assigned variables your query should be as
$sql="INSERT INTO equips (company,street,typeofunit,unitdescription)
VALUES
('".$company."','".$street."','".$typeofunit."','".$unitdescription."')";
You have a , at the end of your query, which is not correct and also you need to use
mysql_real_escape_string() for each Requested data something like
$company = mysql_real_escape_string($_POST['company']);
$street = mysql_real_escape_string($_POST['street']);
$typeofunit = mysql_real_escape_string($_POST['typeofunit'.$i]);
$unitdescription = mysql_real_escape_string($_POST['unitdescription'.$i]);
Also you should start mysqli_* functions or PDO, since mysql_* functions are deprecated.
Upvotes: 2