-1

I am currently learning SQL and have created this code for a few hours but I am unable to add data to my SQL server. Below I have provided a minimum working example of an input field and submit to SQL server. Any help is greatly appreciated. (I removed the password). I believe the problem is with isset($_POST['submit'] or before this point as the server does not seem to acknowledge the rest.

With thanks!

<html>
<body>
<table class="dd" width="100%" id="data">
    <td>Field</td>
    <td>:</td>
    <td width="17%"><input type='textarea' name='field'/></td>
</table>
<input name='submit' type='submit'  value='submit'/>

<?php

// Setting up the Database Connection
$username = "root";
$password = "";
$dbname = "table_name";
$link = mysqli_connect('localhost', $username, $password);

@mysqli_select_db($dbname);

if(isset($_POST['submit']))
    {
    $field = isset ($_POST['field']);
    $field_f = mysql_real_escape_string($field);
    $que = "INSERT INTO table_test ('fieldsql') VALUES ('$field_f')";
    mysqli_query($que);

if (mysqli_query($link,$que) === TRUE) {
echo "Record added successfully";
    }} 
    mysqli_close($link);
?>
</body>
</html>
user44904
  • 1
  • 1

1 Answers1

1

You are missing form in your html. Without form tag your textarea and submit button wont work.

Try this :

<html>
<body>
<form method="post">
<table class="dd" width="100%" id="data">
    <td>Field</td>
    <td>:</td>
    <td width="17%"><input type='textarea' name='field'/></td>
</table>
<input name='submit' type='submit'  value='submit'/>
</form>

Also your query is wrong. Please use following query :

"INSERT INTO table_test (fieldsql) VALUES ('$field_f')";

Side note : Your query is unsafe. Read this How can I prevent SQL injection in PHP?.

Community
  • 1
  • 1
Mr. Engineer
  • 3,522
  • 4
  • 17
  • 34
  • 1
    In addition I think his insert query is not correct (insert into table (field)) and not insert into table('field') – sagi Feb 14 '16 at 17:46