-5
    <html>
    <head>
    </head>
    <body>
    <form action="mysql.php" method="post">
    First Name: <input type="text" name="fname"></br>
    last Name:  <input type="text" name="lname">< </br>
    What is your favrite subject <input type="text" name="subject"></input> </br>
    Your Age : <input type="text" name="age"></input> </br>
    <input type="submit" name="submit" ></input>
    <input type="reset" name="rs"></input>
    </form>
    <?php
    if (isset($_POST\['submit'\]) ){
    $_sa = mysql_connect( "localhost","Ali","pakistan");
    if (!$_sa){ 
        die("can not caonnect".msql_error());
    }

    /*  ----------- condition ----------------------
    if (empty($_POST\[fname\])) {
        echo "First name required";
    }
    ------------------condition end ----------------- */

    mysql_select_db("google", $_sa );

    line 46--  $sql = "INSERT INTO info (firstname,lastname,subject,age) VALUES ('$_POST \[fname\]','$_POST \[lname\]','$_POST \[subject\]', '$_POST \[age\]'')";

    mysql_query($sql,$_sa);
    mysql_close($_sa);

    }

     ?>][1]

// when I run my code its give me error of

Notice: Array to string conversion in C:\xampp\htdocs\mysql.php on line 46

Muhammad Arif
  • 1,014
  • 3
  • 22
  • 56
  • get rid of the \ in `$_POST\['submit'\]` and same for the other ones and extra quote in `[age\]''`. You can also safefy get rid of `` they're not valid closing tags. – Funk Forty Niner Mar 28 '16 at 12:38
  • In other words, your code is stitched with syntax errors. Whoever wants to fix this, please... *be my guest*. ;-) – Funk Forty Niner Mar 28 '16 at 12:41
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Mar 28 '16 at 12:46
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Mar 28 '16 at 12:46
  • *Time to get out the "YIPE!" sign there, right Sam?* - @JayBlanchard – Funk Forty Niner Mar 28 '16 at 12:47
  • *Gravity sucks Ralph!* @Fred-ii- – Jay Blanchard Mar 28 '16 at 12:48
  • 1
    *I can hear the whistle blowin' all the way down Sam.......* @JayBlanchard ♫ ssSSSsssss..... ♫ **CRASH!!!** - *"On with the show this is it"*. ♫ - Ah biddy biddy biddy, that's all folks! – Funk Forty Niner Mar 28 '16 at 12:51

2 Answers2

0

There is an extra single quote written at the end.

WebInsight
  • 307
  • 2
  • 20
  • 1
    At the end of what? There are so many errors in that question, it's not funny. *You call this an answer?* – Funk Forty Niner Mar 28 '16 at 12:45
  • the single quote is after '$_POST \[age\]' on line 46.Please delete it and check – WebInsight Mar 28 '16 at 12:48
  • 1
    There are so many errors that fixing one single quote will not repait the code for the OP. – Jay Blanchard Mar 28 '16 at 12:50
  • Man i know but i had to tell one wrongs out of many and somebody downvoted me.Though i pointed out a mistake.I am not that skilled like some of you. – WebInsight Mar 28 '16 at 12:53
  • Notice: Use of undefined constant fname - assumed 'fname' in C:\xampp\htdocs\mysql.php on line 47 Notice: Use of undefined constant lname - assumed 'lname' in C:\xampp\htdocs\mysql.php on line 47 Notice: Use of undefined constant subject - assumed 'subject' in C:\xampp\htdocs\mysql.php on line 47 Notice: Use of undefined constant age - assumed 'age' in C:\xampp\htdocs\mysql.php on line 47 – ali shahid Mar 28 '16 at 14:31
0

Try this:

$sql = "INSERT INTO info (firstname,lastname,subject,age) VALUES ('{$_POST ['fname']}','{$_POST ['lname']}','{$_POST ['subject']}', '{$_POST ['age']}')";

Explanation:

$_POST is an array So, you need to take care of it while accessing

And, If arrays are accessed inside String, then we should use {} For example: {$_POST['subject']}

Akhil VL
  • 357
  • 1
  • 6
  • Why should the OP "do this"? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. There are so many syntax errors and other errors in the question that fixing the query will be of no use by itself. – Jay Blanchard Mar 28 '16 at 12:49
  • `{}` is required, Try once – Akhil VL Mar 28 '16 at 12:56
  • Whatever, output on `{}` removal: `string(131) "INSERT INTO info (firstname,lastname,subject,age) VALUES ('Array ['fname']','Array ['lname']','Array ['subject']', 'Array ['age']')" ` – Akhil VL Mar 28 '16 at 12:58
  • This line was the code-stopper. Let him take care of the rest – Akhil VL Mar 28 '16 at 13:03
  • 10-4 @AkhilVL! Good call! – Jay Blanchard Mar 28 '16 at 13:04
  • @AkhilVL now my record is adding .. $sql = "INSERT INTO info (firstname,lastname,subject,age) VALUES ('{$_POST [fname]}','{$_POST [lname]}','{$_POST [subject]}', '{$_POST [age]}')"; by doing is – ali shahid Mar 28 '16 at 14:35
  • @AkhilVL the only error is there is this one by its not make any problem but wye is this error is there -------------------Notice: Use of undefined constant fname - assumed 'fname' in C:\xampp\htdocs\mysql.php on line 47 – ali shahid Mar 28 '16 at 14:36
  • @alishahid Write like this: `{$_POST ['fname']}` – Akhil VL Mar 29 '16 at 06:09
  • @AkhilVL its gone ! lol but now its showing data like this in phpmyadmin Array [firstname] old data was there before this {$_POST ['fname']} after this its like Array [firstname] Array [lastname] Array [subject] NULL – ali shahid Mar 29 '16 at 06:28
  • @alishahid Can you write the code here. Only `INSERT INTO info` line – Akhil VL Mar 29 '16 at 06:33
  • @AkhilVL
      $sql = "INSERT INTO info (firstname,lastname,subject,age) VALUES ({$_POST ['fname']},{$_POST ['lname']},{$_POST ['subject']}, {$_POST ['age'] })";
    – ali shahid Mar 29 '16 at 06:41
  • @AkhilVL this one is showing Array [firstname] in the data base – ali shahid Mar 29 '16 at 06:43
  • @alishahid Replace with this: `$sql = "INSERT INTO info (firstname,lastname,subject,age) VALUES ('{$_POST['fname']}','{$_POST['lname']}','{$_POST['subject']}', '{$_POST['age'] }')";` – Akhil VL Mar 29 '16 at 06:48
  • @AkhilVL thank you so much ! it works im new in this soo ! :) i will be in how can i contact you if i need help ? – ali shahid Mar 29 '16 at 06:52
  • @alishahid That would be chargeable! :) – Akhil VL Mar 29 '16 at 07:11
  • amazing that other error are not causing him problem. – Alive to die - Anant Mar 29 '16 at 15:41