mysql not insert data from form

Discuss coding issues, and scripts related to PHP and MySQL.
Posts: 34

mysql not insert data from form

i have two php files:

the index.php ---

Code: Select all

$id ="3";
	if(!empty($_POST["save"])) {
		$conn = mysql_connect("localhost","website_rent","password");
		$itemCount = count($_POST["item_name"]);
		$query = "INSERT INTO preturi (data_i,data_t,zi,noapte,id_proprietate) VALUES ";
		$queryValue = "";
		for($i=0;$i<$itemCount;$i++) {
			if(!empty($_POST["item_name"][$i]))  {
				if($queryValue!="") {
					$queryValue .= ",";
				$queryValue .= "('" . $_POST["data_i"][$i] . "', '" . $_POST["data_t"][$i] . "', '" . $_POST["zi"][$i] . "', '" . $_POST["noapte"][$i] . "', '" . $id . "')";
		$sql = $query.$queryValue;
		if($itemValues!=0) {
			$result = mysql_query($sql);
			if(!empty($result)) $message = "Added Successfully.";
<TITLE>PHP jQuery Dynamic Textbox</TITLE>
<LINK href="style.css" rel="stylesheet" type="text/css" />
<SCRIPT src=""></SCRIPT>
function addMore() {
	$("<DIV>").load("input.php", function() {
function deleteRow() {
	$('DIV.product-item').each(function(index, item){
		jQuery(':checkbox', this).each(function () {
            if ($(this).is(':checked')) {
<FORM name="frmProduct" method="post" action="">
<DIV id="outer">
<DIV id="header">
<DIV class="float-left">&nbsp;</DIV>
<DIV class="float-left col-heading">Item Name</DIV>

<DIV id="product">
<?php require_once("input.php") ?>
<DIV class="btn-action float-clear">
<input type="button" name="add_item" value="Add More" onClick="addMore();" />
<input type="button" name="del_item" value="Delete" onClick="deleteRow();" />
<span class="success"><?php if(isset($message)) { echo $message; }?></span>
<DIV class="footer">
<input type="submit" name="save" value="Save" />
and the input.php file

Code: Select all

<DIV class="product-item float-clear" style="clear:both;">
<DIV class="float-left"><input type="checkbox" name="item_index[]" /></DIV>
<DIV class="float-left"><input type="text" name="data_i[]" /></DIV>
<DIV class="float-left"><input type="text" name="data_t[]" /></DIV>
<DIV class="float-left"><input type="text" name="zi[]" /></DIV>
<DIV class="float-left"><input type="text" name="noapte[]" /></DIV>

but my mysql insert don't work.please help me to find the error

In the php code it is required the element with the name "item_name", but in the <form> there is no such element, there is an <input> with name="item_index".
Try this: in the "input.php file" replace name="item_index[]" with name="item_name[]"

i change this but not work.have you any other ideea?

I not know way it not works; maybe data from the resulted sql query is not ok for that mysql table structure.
If there is no message with errors, try to test the resulted $sql in phpmyadmin.
So, apply: echo $sql; in your code, like the example bellow, copy the sql query from page and test it in phpmyadmin.

Code: Select all

$sql = $query.$queryValue;
echo $sql;
- Also, mysql_connect() is deprecated, it is indicated to write the script to use mysqli or pdo.

Similar Topics