0

I have really interesting problem about insert command.

I have a database consisting of two columns which are "id" and "note" and id is primary key and auto incremented.

BUT

I can't insert anything to "note" though I can insert a num to "id".

echo '<form method="POST">';
echo            '<table>';
echo                '<thead>';
echo                    '<tr>';
echo                        '<th class="genislik100">Yeni Not Ekle</th>';
echo                        '<th></th>';
echo                        '<th class="metinSag"></th>';
echo                    '</tr>';
echo                '</thead>';

echo                '<tbody>';
echo                    '<tr>';
echo                        '<td>Yeni Not:</td>';
echo                        '<td><input type="text" id="note" name="not" /></td>';
echo                        '<td class="metinSag"></td>';
echo                    '</tr>';

echo                    '<tr>';
echo                        '<td><input type="submit" name="submit" Value="Yeni Notu Ekle!!" /></td>';
echo                        '<td></td>';
echo                        '<td class="metinSag"></td>';
echo                    '</tr>';

echo                '</tbody>';
echo            '</table>';
echo                '<form>';




    if (isset($_POST['submit'])){
                        if($_POST['note'] != ''){

                            $note=$_POST['note'];

                            $con=mysql_connect("localhost","root","") or die("ERROR!!");
                            mysql_select_db("db") or die("COULDN'T FIND IT!!") or die("COULDN'T FIND DB"); 

                            $sql="INSERT INTO meteksan_notlar (note) VALUES ('$note')";
                            mysql_query($sql,$con);

                            echo '<script>alert("OKAY!!");</script>';

                            }else
                            echo "<script>alert('FILL THE BLANK!!');</script>";         

                    }

I get "OKAY" alert but when I look to database, there is nothing at "note" column. WHY ??

Please help. Thanks.

Burak
  • 133
  • 2
  • 12

2 Answers2

2

<input type="text" id="note" name="not" />

name="not" should be name="note"

schtever
  • 3,210
  • 16
  • 25
2

Besides the typo in

<input type="text" id="note" name="not" />

which should read as

<input type="text" id="note" name="note" />

Change your code to the following, in order to reflect the deprecation message you received when I told you to add error reporting in comments.

if (isset($_POST['submit'])){
    if($_POST['note'] != ''){

        $note=$_POST['note'];

        $con=mysqli_connect("localhost","root","", "db")  
              or die("Error " . mysqli_error($con));

        $sql="INSERT INTO meteksan_notlar (note) VALUES ('$note')";
        mysqli_query($con, $sql) or die(mysqli_error($con));

        echo '<script>alert("OKAY!!");</script>';

        }else {
        echo "<script>alert('FILL THE BLANK!!');</script>";
        }        

    }


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.


Footnotes:

You can also do: (and with protection against SQL injection)

$note = stripslashes($_POST['note']);
$note = mysqli_real_escape_string($con, $_POST['note']);

$sql="INSERT INTO meteksan_notlar (note) VALUES ('$note')";
$query = mysqli_query($con, $sql) or die(mysqli_error($con));

if($query){
echo '<script>alert("OKAY!!");</script>';
}
else{
echo "There was an error" . mysqli_error($con);
}
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141