Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using php variables inside MySQL insert statement

Tags:

php

mysql

I'm using the following statement, but not sure how to get the $variables inside the statement properly:

mysql_query("INSERT INTO subscribers (email, referral_id, user_id, ip_address)
             VALUES ('$user_email', '$user_refer', '$user_share', '$_SERVER['REMOTE_ADDR']')");
like image 384
stewart715 Avatar asked Nov 04 '11 13:11

stewart715


People also ask

Can you use variables in MySQL?

Mysql also supports the concept of User-defined variables, which allows passing of a value from one statement to another. A user-defined variable in Mysql is written as @var_name where, var_name is the name of the variable and can consist of alphanumeric characters, ., _, and $.

Which function in PHP allows to execute an insert query?

In a database table, the INSERT INTO statement is used to insert new rows. Let's create a SQL query with acceptable values using the INSERT INTO argument, and then execute it by passing it to the PHP mysql query() function to insert data into the table.

What is $variable in PHP?

A variable starts with the $ sign, followed by the name of the variable. A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )


1 Answers

Just change the last one:

mysql_query("INSERT INTO subscribers (email, referral_id, user_id, ip_address)
VALUES ('$user_email', '$user_refer', '$user_share', '".$_SERVER['REMOTE_ADDR']."')");
like image 113
Toto Avatar answered Oct 04 '22 03:10

Toto