2

i'm trying to insert data to MySQL from a dynamic form with jQuery. For the record, I'm using Twitter Bootstrap. I actually achieved insert "rows" but with no data. The best help I found right now is this here for the insert to MySQL and here to jQuery script.

Thanks in advance for taking time for this.

File: add.php

<form  action="../reglas/insert.php" method="post" class="form-horizontal">
<div class="container">
<div id="content">
<div class="row">

<a class="add_line btn btn-small" href="#" style="margin-left:5px">+</a>
</div>

<br>
</div>



</div>

 </div>
 </div>

 <div style="margin:20px auto; width:80px;">

 <input  class="btn btn-success"  type="submit" value="Guardar">
</div>

</form>


<script>
$("#content").delegate(".add_line", "click", function() {
var content = '<div class="row">' +

                     '<div class="span3"><div class="input-append"><input type="text"   class="span2 text-right" placeholder="0.00"  id="enganche"  name="enganche" value="" /><span class="add-on">%</span></div></div>' +
                     '<div class="span3"><div class="input-append"><input type="text" class="span2 text-right" placeholder="0.00" id="comision" name="comision" value="" /><span class="add-on">%</span></div></div>' +
                     '<div class="span3"><div class="input-append"><input type="text" class="span2 text-right" placeholder="0.00" id="r1" name="r1[]" value="" /><span class="add-on">%</span></div></div>' +

                     '<div class="span2"><div class="input-append"><input type="text" class="span1 text-right" placeholder="0.00" id="r2" name="r2[]" value="" /><span class="add-on">%</span></div></div>'+

                     '<a class="del_line btn btn-small" href="#" style="margin-left:5px">-</a>&nbsp;' +
                     '<br><br>'+

                     '</div>';
$("#content").append(content);
return false;   
});

$("#content").delegate(".del_line", "click", function() {
$(this).parent().remove();
return false;  
});
</script>

File: insert.php

The Connection:

$con=mysqli_connect("$host","$username","$password","$db_name");
mysqli_autocommit($con, false);

$flag=true;

The query:

$query="
INSERT INTO $tbl_name(
the_input,
)
VALUES (
'$the_input',
)";

The "for":

for ($i = 0; $i < count($_POST['the_input']); $i++) {
$the_input = $_POST['the_input'][$i];


$result = mysqli_query($con, $query);
if (!$result) {
$flag = false;
echo "Error details: " . mysqli_error($con). ". ";
}
}
if ($flag) {
mysqli_commit($con);
echo "Done!";
}

mysqli_close($con);

?>
8
  • Thanks @leemo, can you be more specific? I really appreciate some guidance here. Commented Nov 28, 2013 at 0:40
  • I'll take a guess at which bits of data you're trying to send and try and mock up an answer for you Commented Nov 28, 2013 at 2:10
  • thanks @leemo. This is how the actual form looks in the browser. (dropbox.com/s/61hxtuls9kwa959/…) Commented Nov 28, 2013 at 2:22
  • can you post the entire HTML of the form? you have this all enclosed in a FORM tag right? Commented Nov 28, 2013 at 2:26
  • also, any particular reason you are using mysql commits? Commented Nov 28, 2013 at 2:28

1 Answer 1

0

Okey dokey.

First things first:

$.delegate() is deprecated so unless you are using a version that is < jQuery 1.7 (which is many versions ago) you should migrate to $.on() event binder/delegator function. This will ensure that your code is future proof (at least until they change the api again!)

Secondly:

I am assuming you have wrapped your data in a form tag and the values are actually getting send to the recieving page here is how I would do it:

if(isset($_POST['the_input']))
{
    //create connection object;
    $mysqli = new mysqli($host,$username,$password,$db_name);

    $query = "INSERT INTO `the_input` VALUES (";

    //construct the query. Get the the_input data and paramater string (required for stmt binding)
    foreach($_POST['the_input'] as $value)
    {
        $query .= $value . ",";
    }

    //remove trailing comma
    $query = substr($query, 0, strlen($query)-1);
    $query .= ")";

    //this is just so you can see what the loop did and test the output of the query, remove this when you're confident
    echo "The constructed query string is: " . $query

    if($mysqli->query($query) == false)
    {
        trigger_error($mysqli->error);
    } else {
        echo "Success! Check the DB!";
    }

} else {
    Echo "No POST data was recieved";
}
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you very much @leemo for the advise and the answer. let's try it!
@HcH there was an error in my code that I have now corrected... a missing bracket.
Thanks @leemo I have this error. I'm using Adobe Dw. dropbox.com/s/bgp5ukcw4ihom8o/…
maybe dreamweave doesnt like that syntax... did you try running it on the server? I have updated my answer to provide and alternative
yeah,i have this... Parse error: syntax error, unexpected T_IF, expecting ',' or ';' in /home/content/07/11731907/html/reglas/insert.php on line 32
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.