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"> </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
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
i change this but not work.have you any other ideea?
Admin
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
-
Get ID after insert with PDO
PHP - MySQL
First post
Hello Coursesweb,
I asking for longer time myself how to get the ID after an insert into database with pdo.
<?php
if(isset($_POST )){...
Last post
To get the id after an Insert with pdo, use the lastInsertId() method.
$sql = INSERT INTO tracker_comment...
-
PDO statement Insert Into with where not
PHP - MySQL
First post
Chef hope you do fine.... I readed many documents about PDO insert into, mostly with the function where not exists.
I tried also some things into...
Last post
The query: Insert into ... where not exists ... is used when you want to insert something in database when a specified data not exists in that table....
-
Add results from database in form address
PHP - MySQL
First post
Hello Coursesweb
I have a problem to get results from the database into input type text form in the place name.
name= id='. $row1 ....
Last post
Marplo thanks thanks thanks for all suport
& even so quick suport `thanks again`
it is working
I could kiss you :D
-
Data from SQL in alphabetical order
PHP - MySQL
First post
Plessant Coursesweb,
I have an grouplist in my database from country`s.
First time I did not do any search-study for this questionm but ask you...
Last post
@HayatAnsiklopedisi , post the SQL query you have, and explain what result you want to get.
-
Delete Data without Refreshing Page
JavaScript - jQuery - Ajax
First post
Happy New Year Chief.... My first two weeks I spended on bed due to illness
I wanna use a script but it does not delete the item from my sql...
Last post
O God that I did not see this `stupido me`....
Thank you very much for all the support `its working now`