mysql connect error issue
- by Alex
I've php page which update Mysql Db. I don't understand why my following php code is saying that "Could not update marker. No database selected". Strange!! can you please tell me why it's showing error message.
Thanks.
Php code:
<?php
// database settings
$db_username = 'root';
$db_password = '';
$db_name = 'parkool';
$db_host = 'localhost';
//mysqli
$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name);
if (mysqli_connect_errno())
{
header('HTTP/1.1 500 Error: Could not connect to db!');
exit();
}
################ Save & delete markers #################
if($_POST) //run only if there's a post data
{
//make sure request is comming from Ajax
$xhr = $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
if (!$xhr){
header('HTTP/1.1 500 Error: Request must come from Ajax!');
exit();
}
// get marker position and split it for database
$mLatLang = explode(',',$_POST["latlang"]);
$mLat = filter_var($mLatLang[0], FILTER_VALIDATE_FLOAT);
$mLng = filter_var($mLatLang[1], FILTER_VALIDATE_FLOAT);
$mName = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$mAddress = filter_var($_POST["address"], FILTER_SANITIZE_STRING);
$mId = filter_var($_POST["id"], FILTER_SANITIZE_STRING);
/*$result = mysql_query("SELECT id FROM test.markers WHERE test.markers.lat=$mLat
AND test.markers.lng=$mLng");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
$id=$row[0];*/
//$output = '<h1 class="marker-heading">'.$mId.'</h1><p>'.$mAddress.'</p>';
//exit($output);
//Update Marker
if(isset($_POST["update"]) && $_POST["update"]==true)
{
$results = mysql_query("UPDATE parkings SET latitude = '$mLat', longitude
= '$mLng' WHERE locId = '94' ");
if (!$results) {
//header('HTTP/1.1 500 Error: Could not Update Markers! $mId');
echo "coudld not update marker." . mysql_error();
exit();
}
exit("Done!");
}
$output = '<h1 class="marker-heading">'.$mName.'</h1><p>'.$mAddress.'</p>';
exit($output);
}
############### Continue generating Map XML #################
//Create a new DOMDocument object
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers"); //Create new element node
$parnode = $dom->appendChild($node); //make the node show up
// Select all the rows in the markers table
$results = $mysqli->query("SELECT * FROM parkings WHERE 1");
if (!$results) {
header('HTTP/1.1 500 Error: Could not get markers!');
exit();
}
//set document header to text/xml
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while($obj = $results->fetch_object())
{
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name",$obj->name);
$newnode->setAttribute("locId",$obj->locId);
$newnode->setAttribute("address", $obj->address);
$newnode->setAttribute("latitude", $obj->latitude);
$newnode->setAttribute("longitude", $obj->longitude);
//$newnode->setAttribute("type", $obj->type);
}
echo $dom->saveXML();