0

I'm using PHP and MySQL. I am trying to insert form data from a web page to a MySQL database using a PHP function.

This is the code of the user form page:

<?php 
include 'core/int.php';
include 'includes/overall/header.php';


if(!empty($_POST)){
$add_status = $insert->add_status($user_data['user_id'], $_POST);
}
?>


  <form action = "" method = "POST">
        <ul>
            <li>
                <input name = "question_time" type = "hidden" value = "<?php echo time()?>" />
            </li>
            <li>
                <h2>Post your Question <?php echo $user_data['username'];?></h2>
                <textarea name = "question"></textarea>
            </li>
            <li>
                <p><input type = "submit" value = "submit"></p>
            </li>
        </ul>
  </form>

and this is the function 'add_status' i am defining on another page..

function add_status($user_id, $_POST){

mysqli_query("INSERT into `post` (user_id, status_time, status_content) VALUES($user_id, '$_POST[question_time]', '$_POST[question]')");
}

When this runs on localhost it throws an error:

Cannot re-assign auto-global variable _POST in C:\wamp\www\zr\core\functions\users.php on line 97

I see that Line 97 is function add_status()

What does the error mean and how do I fix it?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
shivam
  • 383
  • 8
  • 22

4 Answers4

2

Your problem is this

function add_status($user_id, $_POST){

You're declaring $_POST to be a local argument variable. By definition, $_POST is a superglobal (available to all scopes). So either you need to remove the argument and just let it propagate normally or you need to change it to a local variable like $post and use that instead.

function add_status($user_id, $post){

I would recommend the latter.

Some other notes: mysql_query and the associated arguments are deprecated and will be removed. Consider mysqli instead.

You're passing $_POST directly into SQL. That leaves you wide open to SQL injection. You need to fix that.

Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • ya, I got this one thanks a lot.. But now there is a new error...you can see in the edited version... – shivam Mar 27 '15 at 17:45
  • @shivam You need to ask a new question then. Don't keep editing this question with new problems after that one has been resolved. – Machavity Mar 27 '15 at 18:16
1

You cannot use $_POST as another local variable because that is defined in php as a global variable with read-only attribute. To solve the problem, just use another variable name instead of $_POST in the function definition add_status. Ex:

function add_status($user,$postVar){
    ...
}
akshay
  • 472
  • 4
  • 14
1

$_POST is a global variable, and as is it says in the error you cannot reassign it. You mus change you function declaration by

function add_status($user_id, $values){
  mysql_query("INSERT into `post` (user_id, status_time, status_content) VALUES($user_id, '$values[question_time]', '$values[question]')");
}

@jens says :

Do not use the depricated myslq_* API. Use mysqli_* or pdo with prepared statements – Jens

you should replace that to

Tit-oOo
  • 208
  • 1
  • 9
1

use following code

function add_status($user_id){
$qt = $_POST[question_time];
$q=$_POST[question];
mysqli_query("INSERT into `post` (user_id, status_time, status_content) VALUES($user_id, '$qt', '$q')");
}

It should work for you.

Ashutosh Nigam
  • 868
  • 8
  • 27