Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to PHP. I wanted to create a new record in another table but just one new variable gets returned. I've tried following:

$user_id = mysql_real_escape_string($_POST['user_id']);
$user_name = mysql_query("SELECT user_name FROM accept WHERE user_id=".$user_id." ");
$row1 = mysql_fetch_array($user_name);
$server = mysql_query("SELECT server FROM accept WHERE user_id=".$user_id." ");
$row2 = mysql_fetch_array($server);
$url = mysql_query("SELECT link FROM accept WHERE user_id=".$user_id."");
$row3 = mysql_fetch_array($url);
$lpoints = mysql_real_escape_string($_POST['lpoints']);

And my result is this. enter image description here

share|improve this question
4  
mysql_fetch_array() returns an array, not a scalar value. You are inserting those arrays directly, which PHP represents as Array. You mean to do things like $row3 = mysql_fetch_array($url); $url = $row3['url']; However, you should get all of these in one query. SELECT user_name, server, link FROM accept WHERE user_id = $user_id –  Michael Berkowski Aug 2 '14 at 21:48
    
Use var_dump($row1) for example to see what the return looks like. –  Michael Berkowski Aug 2 '14 at 21:48
1  
Now, all that said, new code should not be written with the mysql_*() functions. They are deprecated and will soon be removed from PHP. Instead, you should begin using PDO or MySQLi, with prepared statements. See this question for practical examples of both. This excellent PDO tutorial places PDO in context of the mysql_*() functions if you're already familiar with those. –  Michael Berkowski Aug 2 '14 at 21:49

3 Answers 3

Try This-

$user_id = mysql_real_escape_string($_POST['user_id']);
$result = mysql_query("SELECT user_name, server, link FROM accept WHERE user_id=".$user_id." ");
$row=mysql_fetch_array($result)
$row1=$row['user_name'];
$row2=$row['server'];
$row3=$row['link'];
$lpoints = mysql_real_escape_string($_POST['lpoints']);

Now you got what you wanted based on your requirement use the data to insert or update.

share|improve this answer

You should use pdo or mysqli and here is your code;

  $user_id = &$_POST["user_id"];
    if($user_id){
        $result = mysql_query("select user_name,server,link,lpoints from accept where user_id='".mysql_real_escape_string($user_id)."'");
        /*You should use single quotes for escaping sql injection*/
        if($result){
            $vars = mysql_fetch_array($result);
            if($vars){
                list($username,$server,$link,$lpoints) = $vars;
            }
            else{
                //do something with errors
            }
            mysql_free_result($result);
        }
        else{
            //do something with errors
        }
    }
    else{
        //do something with errors
    }
share|improve this answer

First of all, combine your queries into one:

$user_id = mysql_real_escape_string($_POST['user_id']);
$user_info = mysql_query("SELECT user_name, server, link FROM accept WHERE user_id=".$user_id." ");
$row = mysql_fetch_array($user_info);
$lpoints = mysql_real_escape_string($_POST['lpoints']);

In order to create a new record, you will need INSERT INTO, to change existing records use UPDATE.

When you're fetching info from the database, it will be an array so you will need to use it accordingly. So essentially, to use the variables it will be like this:

$row['user_name'] or $row['server'] etc..

Also, look into using mysqli instead. You will need to change your connection script and some other syntax but it needs to be done. mysql is deprecated, insecure, and future support is not there so you will need to change it later anyway.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.