mysql not insert data from form

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

mysql not insert data from form

i have two php files:

the index.php ---

Code: Select all

<?php
$id ="3";
	if(!empty($_POST["save"])) {
		$conn = mysql_connect("localhost","website_rent","password");
		mysql_select_db("website_rent",$conn);
		$itemCount = count($_POST["item_name"]);
		$itemValues=0;
		$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]))  {
				$itemValues++;
				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.";
		}
	}
?>
<HTML>
<HEAD>
<TITLE>PHP jQuery Dynamic Textbox</TITLE>
<LINK href="style.css" rel="stylesheet" type="text/css" />
<SCRIPT src="http://code.jquery.com/jquery-2.1.1.js"></SCRIPT>
<SCRIPT>
function addMore() {
	$("<DIV>").load("input.php", function() {
			$("#product").append($(this).html());
	});	
}
function deleteRow() {
	$('DIV.product-item').each(function(index, item){
		jQuery(':checkbox', this).each(function () {
            if ($(this).is(':checked')) {
				$(item).remove();
            }
        });
	});
}
</SCRIPT>
</HEAD>
<BODY>
<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>
<DIV id="product">
<?php require_once("input.php") ?>
</DIV>
<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>
<DIV class="footer">
<input type="submit" name="save" value="Save" />
</DIV>
</DIV>
</form>
</BODY>
</HTML>
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>

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

Admin Posts:805
Hello,
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[]"

mlucicom Posts:37
i change this but not work.have you any other ideea?

Admin Posts:805
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.