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.

6
  • Ya gotta love it when you hear the echos of the birds in the mountains. But too many of them in the city, gets a car dirty and fast. Commented Jan 25, 2015 at 1:08
  • what should I get from this special sentence ? :D Commented Jan 25, 2015 at 1:09
  • It means doing all those echos is pointless and more coding for nothing. Add or die(mysql_error()) to mysql_query() to find out what errors out. Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); see if it yields anything. Doing that will help you spot the errors, of Undefined index. You can do one echo with all this, or none at all and just seperate HTML from PHP. Commented Jan 25, 2015 at 1:10
  • Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\xampp\htdocs\meteksan\admin_sayfasi.php on line 447 I get this Commented Jan 25, 2015 at 1:13
  • 1
    There you go, time to switch ;-) Commented Jan 25, 2015 at 1:16

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.