简体   繁体   中英

Mysql query in php of webserver

This is the php java script to run on webserver. In it i will get input from android app the query is

<?php
$mysqlDebug = true;

//create connection

print "hello 1";

mysql_connect("localhost", "pu", "Ind");

print "hello 2";

mysql_select_db("leengage");

print "hello 3";
print mysql_error();
// Check connection

if (mysql_error())
{
    echo "Failed to connect to MySQL: " . mysql_error();
    print "hello 4";
}

print "hello 5";

mysql_query("INSERT INTO details_master(name ,phone, email, birthdaydate, birthdaymonth, anniversarydate, anniversarymonth,created)
VALUES ('Peterr', 'Griffinn','hello@hello.com',12,12,12,12,'0000-00-00 00:00:00')");

print mysql_error();

// escape variables for security

print "hello 7";

$name = mysql_real_escape_string($conn, $_POST['name']);
$phone = mysql_real_escape_string($conn, $_POST['phone']);
$email = mysql_real_escape_string($conn, $_POST['email']);
$birthdaydate = mysql_real_escape_string($conn, $_POST['birthdaydate']);
$birthdaymonth = mysql_real_escape_string($conn, $_POST['birthdaymonth']);
$anniversarydate = mysql_real_escape_string($conn, $_POST['anniversarydate']);
$anniversarymonth = mysql_real_escape_string($conn, $_POST['anniversarymonth']);
$created = mysql_real_escape_string($conn, $_POST['created']);

$sql = "INSERT INTO details_master(name ,phone, email, birthdaydate, birthdaymonth, 
anniversarydate, anniversarymonth,created)
VALUES ('$name','$phone', '$email', '$birthdaydate', '$birthdaymonth', '$anniversarydate', 
'$anniversarymonth', '$created')";

print "hello 8";

if (!mysql_query($conn, $sql))
{
    die('Error: ' . mysql_error($conn));
}

echo "1 record added";
?>

The first insert query is running fine and inserting records from app but second insert query is not working it gives following errors.

[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 24

[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 25

[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 26

[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 27

[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 28

[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 29

[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 30

[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given in /home on line 31

[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_query() expects parameter 2 to be resource, string given in /home on line 37

[01-Aug-2014 05:44:53 America/Denver] PHP Warning: mysql_error() expects parameter 1 to be resource, null given in /home on line 38

Just in case I am getting input from android app and code i am using for this is on create:

StrictMode.ThreadPolicy policy = new 
StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy); 

And on button click is:

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

// define the parameter
postParameters.add(new BasicNameValuePair("name",name));
postParameters.add(new BasicNameValuePair("phone",phone));
postParameters.add(new BasicNameValuePair("email",email));
postParameters.add(new BasicNameValuePair("birthdaydate",s1));
postParameters.add(new BasicNameValuePair("birthdaymonth",s2));
postParameters.add(new BasicNameValuePair("anniversarydate",s3));
postParameters.add(new BasicNameValuePair("anniversarymonth",s4));
postParameters.add(new BasicNameValuePair("created",formattedDate));

String response = null;

try {
    response = CustomHttpClient.executeHttpPost("http://purplefront.net/PurpleEngage/mydatainsert.php", postParameters);

    // store the result returned by PHP script that runs MySQL query
    String result = response.toString();  

    //parse json data
    Log.d("result data", result);
}
catch (Exception e) {
    Log.e("log_tag","Error in http connection!!" + e.toString());     
}  

You keep referencing $conn , which doesn't appear to be set anywhere. It looks like you're trying to use mysqli_ syntax (connection object, then sql) with mysql_ functions (which use the last opened connection)

This answer assumes you convert all your calls to mysqli_ (you can't mix and match) but this should point you in the right direction

$conn = mysqli_connect("localhost","pu","Ind");
$name = mysqli_real_escape_string($conn, $_POST['name']);
if (!mysqli_query($conn,$sql)) {
    die('Error: ' . mysqli_error($conn));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM