0

I want to create a table that can add a new row when click a button or link, and then let user input and submit the value to php and store it in database.

Problem 1:

In my HTML,

<script>
    document.getElementById('table').innerHTML +=
    "<td><textarea id='addRow' name='addRow'></textarea></td>";

    var addRow = document.getElementById('addRow').innerHTML;     <-- Is this correct? Because I'm unable to get the data, tried with .value and .innerHTML.

    document.getElementById('table').innerHTML +=
    "<td><a href='page2.php?addRow=" + addRow + "'>Add</a></td>";
</script>

It's work fine when I add a row, by when I press the Add generated by the script, it's didn't obtain the value I input into the textarea.

Problem 2 :

The link generated from the javascript, when it is clicked, it submit an address like this :

page2.php?addRow=

The address consists of empty data, but it should be save in the page2.php too because I do not set any checking for the value.

Here is my page2.php code :

<?php
    require("Connections/localhost.php");

    $addRow = $_REQUEST["addRow"];          <-- I tried with $_GET, $_POST, $_REQUEST.
    $query = "INSERT INTO row2 (addRow) VALUES ($addRow)";
    $result = mysql_query($query);

    if($result)
    header("Location:Home.php");

?>

What is the error of my code?

EDIT :

(12/11/2012) I have uploaded my code to here. How can I solve this problem?

(17/11/2012) The MySQL part, there is some syntax error I made, that is I forget to put the single quote ' ' for each VALUES.

Solved :

I change the href to another javascript function.

"<td><h4><a href='javascript:send()'>Add</a></h4></td>"

In the send(), I get the textarea value and submit it with window.location(). That's solve my problem.

May be my code is not the appropriate way to do it, but I'll try to improve next time.

13
  • 1
    I bet you have multiple rows with the same ID don't you? Also, that is some sexy SQL Injection vulnerable code you got there. Commented Nov 14, 2012 at 2:04
  • Please, put down the deprecated mysql_query function and step away from it slowly. You're going to cause yourself some severe harm if you stay on this reckless path. You must always employ proper SQL escaping when writing queries. New applications should use PDO or mysqli. Commented Nov 14, 2012 at 2:16
  • Since you're just getting started, you might want to try one of the popular PHP frameworks like CakePHP, CodeIgnighter or FuelPHP to give your application some structure and a lot more capability than you could create from the ground up like you're trying to do here. Code like what you've created here is a sterling example of why frameworks are essential to creating reliable applications and being productive as a programmer implementing your specific logic and not wasting time on already solved problems. Commented Nov 14, 2012 at 2:19
  • @Chad, the id addRow only declared once, which is in the script. By the way, what do you meant by some sexy SQL Injection? @@ @tadman, yes, I'm just getting started, and the SQL code I'm refer to my reference book and my lecturer's note. Commented Nov 14, 2012 at 2:23
  • If your lecturer is teaching you mysql_query you're learning an obsolete interface from the 1990s. Considering PDO only takes a half hour to learn, this is inexcusable. Please read up on SQL injection bugs to see how dangerous this kind of code is. Also, for your own sake, pick a framework and learn it well. You'll be massively more productive. Commented Nov 14, 2012 at 2:27

0

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.