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.
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 ormysqli
.addRow
only declared once, which is in the script. By the way, what do you meant bysome 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.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.