The same script in PHP in localhost doesn't working online - php

I upload my script in my server and I made a new database in the same server.
the connection with database is working, but when the two script check or insert new value on the database doesn't working, but in localhost yes!!
<?php
require 'client/facebook.php';
$app_id = "***";
$secret = "***";
$app_url = "***";
// Create our Application instance
///jump the code
////////////////////////////////
$sdb = "***";
$db = "***";
$userdb = "***";
$passdb = "***";
$dblink = mysql_connect($sdb,$userdb,$passdb);
$seldb = mysql_select_db($db);
$username = $username; //user data
$UIDfaceboook =$id; //UID USER FACEBOOK FROM API
$user_type ="aa"; //USER DATA
$connection =""; //variable for start function check UID
$checkUIDdb ="INATTESA"; //check if exist the UID facebook
$insertnewuser ="";
$loadspecific ="inattesa";
///
///CHECK CONNECTION WITH DATABASE
///
$mysqlConnection = mysql_connect($sdb, $userdb, $passdb);
if (!$mysqlConnection)
{
echo "NO DATABASE FOUND, CHECK USER, PASS, DB";
}
else
{
echo "connection with database is ";
echo $connection = "ok";
mysql_select_db($db, $mysqlConnection);
}
//////////////////////////THIS FUNCTION DOESN'T WORK ONLINE
if ($connection = "ok"){
$con=mysqli_connect($sdb,$userdb,$passdb,$db);
$check="SELECT * FROM tabletest WHERE UIDfacebook = '$UIDfaceboook'";
$rs = mysqli_query($con,$check);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
if($data[0] > 1) {
echo "UID IN THE DATABASE ";
echo $checkUIDdb = "found";
}
else
{
echo "UID IN THE DATABASE ";
echo $checkUIDdb = "nofound";
}
}
///////////////THIS FUNCTION DOESN'T WORK ONLINE, and doesn't insert values
if($connection == "ok" && $checkUIDdb == "nofound"){
$username = strip_tags(mysql_real_escape_string($username));
$UIDfacebook = strip_tags(mysql_real_escape_string($UIDfaceboook));
$user_type = strip_tags(mysql_real_escape_string($user_type));
$sql = mysql_query("INSERT INTO `$db`.`tabletest` (`id`,`username`,`UIDfacebook`,`user_type`) VALUES ('','$username','$UIDfaceboook','$user_type');");
if($sql){
//The query returned true - now do whatever you like here.
echo $connection = "SAVE USERNAME, UID FACEBOOK AND USER TYPE ON DATABASE";
echo $loadspecific ="caricadati";
echo $insertnewuser = "yes";
}else{
//The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
}
}else{
echo $connection = " CORRECT LOGIN WITH FACEBOOK";
}
mysql_close($dblink);
//Close off the MySQL connection to save resources.
?>
the echo php on my server said " connection with database is okUID IN THE DATABASE nofound" so, I don't understand when the variable $connect, $checkUIDdb is working don't load the script for insert a new user... I try to add my UID facebook in my database to check if the script jump this step, but the script ignore the database... just said that...
maybe the first problem is on $check="SELECT * FROM tabletest WHERE UIDfacebook = '$UIDfaceboook'";
but in localhost works..
thank you very much, i don't know what I can do...

first error i saw was this one:
//////////////////////////THIS FUNCTION DOESN'T WORK ONLINE
if ($connection = "ok"){
i guess you should edit this to
if ($connection == "ok"){
so you ask if $connection has the value "ok" and not if the allocation of "ok" to $connection has been done successfully.
Second error could be a mixture of "mysql" and "mysqli"...
hth

Related

SQL insert to MySQL doesn't work

I'm currently trying to insert username, pw to a DB, and check if the username already exists.
The problem is that the SQL (select) syntax doesn't work, nor does the (insert). I've checked around for a couple of hours in forums and Stackoverflow, and my current code is the following.
What might be the problem?
Thanks, Jimmie.
<?php
$servername = "localhost";
$username = "name";
$password = "pw";
$dbname = "dbaname";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ((isset ($_POST["identity"])) && (isset ($_POST["pin"])) && (isset ($_POST["token"])))
{
$identity = htmlspecialchars($_POST['identity'], ENT_QUOTES, "ISO-8859-1");
$pin = htmlspecialchars($_POST['pin'], ENT_QUOTES, "ISO-8859-1");
$token = htmlspecialchars($_POST['token'], ENT_QUOTES, "ISO-8859-1");
echo "$identity";
if($token == "xyz13D;A##:!#")
{
$result = $mysqli->query("SELECT `identity` FROM Users WHERE `identity` = '" . $identity . "'");
if($result->num_rows == 0)
{
echo "successCreat";
// Perform queries
mysqli_query($mysqli,"SELECT * FROM Users");
mysqli_query($mysqli,"INSERT INTO Users (identity,pin,userActivity, identityCreated) VALUES ('$identity', '$pin',1,now())");
}
else
{
echo "failureCreate";
}
}
else
{
echo"Wrong Key";
}
}
$mysqli->close();
?>
Assuming that identity is a primary key, then you can check the error flags after executing an INSERT query to see if an error occurred.
mysqli_query( $mysqli, "INSERT INTO ... " ); //< ... Represents query
if (mysqli_error( $mysqli )) {
echo "Failure";
}
else {
echo "Success";
}
Also, you should properly escape input as stated in the comments. In addition, you should check whether or not the connection attempt was successful using mysqli_connect_error.
Finally, there might be an issue in your SQL suntax which mysqli_error will also catch. A last possibility is that the POST data isn't being set properly and the code is being ignored completely.

PHP My Select isn't working?

I believe my PHP to be functioning perfectly, therefore I think it's a query error. When I proceed, with form details stored in the session... it happily returns my Posted information but doesn't seem to be pulling anything from my database - there is a row in my database containing the email address I am using. Does anybody see anything blatantly wrong with this PHP?
Thanks for your help.
<?php
session_start();
$servername = "localhost";
$username = "privatedbroot";
$password = "not4ulol";
$dbname = "pdb_inventory";
$status = $_GET["action"];
$_SESSION["Cemail"] = $_POST["CEMAIL"];
$_SESSION["Access"] = md5($_POST["ACCESS"]);
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT CEMAIL, ACCESS FROM POPU WHERE `CEMAIL`= ".$_SESSION['Cemail'];
echo $sql;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($_SESSION["Access"] == $row["ACCESS"]){
echo "password correct!";
} else {
echo "password wrong!";
}
}
}else{
echo "ur email is wrong m8.";
}
?>
Try this:
$cemail = $_SESSION['Cemail'];
$sql = "SELECT CEMAIL, ACCESS FROM POPU WHERE `CEMAIL`= '$cemail'";

PHP error in mysql database selection

I wrote a code in PHP to connect with Mysql database and add data into it.. code is given below:
$dbhostname = "localhost";
$dbusername = "root";
$dbpassword = "root";
$dbname = "rms_invoice";
function showDBMessage(){
if($_POST["name"] != NULL){
//came from register page
if(register()){
print("<br/>Registered successfully!!!");
} else {
print("<br/>Registration failed!!!");
}
}
}
function register(){
global $dbhostname,$dbusername,$dbpassword,$dbname;
// Create connection
$con=mysqli_connect($dbhostname,$dbusername,$dbpassword);
// Check connection
if (!mysqli_connect_errno()) {
print("MYSQL Connection established...<br/>");
echo "<br/>Successful: ".mysqli_get_host_info($con);
$selected = mysqli_select_db($con, $dbname);
if(false!==$selected){
$name = mysqli_real_escape_string($con, $_POST["name"]);
$username = mysqli_real_escape_string($con, $_POST["username"]);
$password = mysqli_real_escape_string($con, $_POST["password"]);
$address = mysqli_real_escape_string($con, $_POST["address"]);
$insertUserQuery="INSERT INTO USER(name,username,password,address) VALUES ('$name','$username','$password','$address')";
echo $insertUserQuery;
$result = mysqli_query($con, $insertUserQuery);
if ( false===$result ) {
printf("<br/>mysqli_connect_error: %s\n", mysqli_connect_error());
printf("<br/>mysqli_error: %s\n", mysqli_error($con));
} else {
echo "<h4>New User Added!!!</h4><br/>";
return true;
}
}else{
printf("<br/>Error while selecting database, error:%s\n",mysqli_errno($con));
}
} else {
print("<br/>Failed to connect to MySQL: "+mysqli_connect_error());
}
mysqli_close($con);
return false;
}
and the output is:
MYSQL Connection established...
Successful: localhost via TCP/IP
INSERT INTO USER(name,username,password,address) VALUES ('nitin','ndthokare','p','beed')
mysqli_connect_error:
mysqli_error: No database selected
Registration failed!!!
It is getting connected but database selection seems to be problem. (same query executes successfully through mysql cli).
I tried many possible corrections in code discussed on SO, but could not succeed.
Can anybody help me to find out the mistake?
if(false!==!$selected){
is extremely unusual operator (called "double negative") means you want to execute the following code block only if $selected is FALSE.
to make it much less confusing, write it without all these magic chants
if($selected){
or even omit this useless check completely.

No error, but still can't use 3 stored procedure in 1 pages php using database mysql

I have a script that checks an IP address from database, then checks if the IP address is unique or not. If the IP address is not unique, it saves the IP Address to IP_history on database. It's working very slowly, that's why I converted it to mysql stored procedure, but it doesn't run the same. It only runs the 1st procedure. I need it to run 3 procedures in a single page.
Here is my code:
$server = 'localhost'; // MySQL hostname
$username = 'xxx'; // MySQL username
$password = 'xxx'; // MySQL password
$dbname = 'xxx'; // MySQL db name
//$ipvisitor=$_SERVER['REMOTE_ADDR'];
if (isset($_GET['ip']) && $_GET['ip']) {
$ipvisitor = $_GET['ip'];
} else {
$ipvisitor = $_SERVER['REMOTE_ADDR'];
}
$db = new mysqli($server, $username, $password, $dbname);
if ($db->connect_errno) {
echo $db->connect_error;
}
if ($result = $db->query("CALL cekip('$ipvisitor')")) {
$db->error;
}
$dataip = $result->fetch_array();
$hitung = $result->num_rows;
if ($hitung == 0) {
echo "Tidak Ada Dalam Database";
} else {
$cekunik = $db->query("CALL cekhistory('$ipvisitor')");
$unik = $cekunik->num_rows;
if ($unik > 0) {
echo "Sudah Pernah Masuk";
} else {
echo $dataip['country'];
$addhistory = $db->query("CALL addhistory('$ipvisitor')");
}
}
Why is it only running one call?
And what is the best approach to fix the issue?

New $_SESSION variable not created after query?

I'm trying to build a login process where, by using $_SESSION variables, the login credentials of the user are stored and used to show their relevant data from the database on screen (i.e. they will only see the school data that they work for).
<?php
session_start();
if(!isset($_SESSION['Initials'], $_SESSION['Surname']))
{
$host = "xxx";
$username = "xxx";
$password = "xxx";
$database_name = "xxx";
$table_name = "xxx";
mysql_connect($host, $username, $password) OR die("Can't
connect");
mysql_select_db($database_name) OR die("Can't connect to
Database");
$query = "SELECT Class FROM $table_name WHERE Initials = '".
$_SESSION['Initials']."' AND staff LIKE '%".$_SESSION['Surname']."'";
$result = mysql_query($query);
$class = mysql_fetch_array($result);
$count = mysql_num_rows($result);
if($count === NULL)
{
echo "ERROR";
}
else
{
$_SESSION['Class'] = $result;
echo "Class added to sessions";
}
}
?>
My initial problem where the query couldn't recognize the session variables was easily solved by adding the correct brackets for the if-statement. My next problem that has arisen here is that even though the query should be successfull (I don't receive an error message saying 'ERROR' when the $count is either FALSE or NULL) it's not creating the result array into a new session, because when I print the session array on a new page it's still only carrying over the 'Initials' and 'Surname' sessions.
What do I need to change to my query, or post-query process in order for that array (because it's bound to throw up multiple results) to be made into a new session?
Many thanks for the answers to my initial problem!
if(!isset($_SESSION['Initials'], $_SESSION['Surname'])) {
// code
}
u need { } brackets
if(!isset($_SESSION['Initials'], $_SESSION['Surname']))
$host = "xxxxx"; $username = "xxxxx"; $password = "xxxxx";
is
if(!isset($_SESSION['Initials'], $_SESSION['Surname'])) {
$host = "xxxxx";
}
$username = "xxxxx";
$password = "xxxxx";
I've found the answer - it turned out that I wasn't treating one of the session variables as a proper array and thus wouldn't load properly. I've added my script below so that people with similar problems in the future can use it as a reference point:
<?php
session_start();
// Server Details //
$host = "---";
$username = "---";
$password = "---";
$database_name = "---";
$table_name = "---";
// Connect Command //
mysql_connect($host, $username, $password) OR die("Can't
connect");
mysql_select_db($database_name) OR die("Can't connect to
Database");
// Query to call up the unique school name //
$query_school = mysql_query("SELECT DISTINCT School FROM $table_name
WHERE Initials = '".$_SESSION['---']."'
AND staff LIKE '%".$_SESSION['---']."'") or die( mysql_error());
$result_school = mysql_result($query_school, 0);
// Query to call up the unique centre no //
$query_centreno = mysql_query("SELECT DISTINCT CentreNo FROM
$table_name WHERE Initials = '".$_SESSION['---']."'
AND staff LIKE '%".$_SESSION['---']."'") or die( mysql_error());
$result_centreno = mysql_result($query_centreno, 0);
// The newly created sessions for school info //
$_SESSION['---'] = $result_school;
$_SESSION['---'] = $result_centreno;
// Query to call up the array of classes //
$query_class = mysql_query("SELECT Class FROM $table_name WHERE
Initials = '".$_SESSION['---']."'
AND staff LIKE '%".$_SESSION['---']."'") or die( mysql_error());
$query_class__array = array();
while($row = mysql_fetch_assoc($query_class))
$query_class_array[] = $row;
$_SESSION['---'] = $query_class_array;
?>

Categories