Im trying to update a database using php.
The site im working on , i have a local copy and a copy on a server.
It works locally , but on the server it shows that it worked , but does not update the database.
I have checked that the id and data variables do actually have a value and teh connection is working.
This is my code :
<?php
include("../../config/connect.php");
if($_GET['id'] and $_GET['data'])
{
$id = $_GET['id'];
$data = $_GET['data'];
$key = $_GET['key'];
echo $id;
echo "<br>";
echo $data;
echo "<br>";
echo $key;
echo "<br>";
if(mysqli_query($connection, "update userbadges set level='$data' where id='$id'"));
echo 'success';
}
?>
Edit Connect.php
<?php
$connection = mysqli_connect('localhost', 'myusername', 'mypassword', 'leaderboard');
?>
You need to be checking everything to do with the mysqli_ calls
connect.php
<?php
$connection = mysqli_connect('localhost', 'myusername',
'mypassword', 'leaderboard');
if (!$connection) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
Your code:
<?php
include("../../config/connect.php");
if( isset($_GET['id'], $_GET['data'], $_GET['key']) ) {
$data = $_GET['data'];
$id = $_GET['id'];
$key = $_GET['key'];
echo $id;
echo "<br>";
echo $data;
echo "<br>";
echo $key;
echo "<br>";
$result = mysqli_query($connection,
"update userbadges set level='$data' where id='$id'");
if(! $result) {
echo mysqli_error($connections);
exit;
}
echo 'database update ' . mysqli_affected_rows($connection) . ' rows';
} else {
echo 'One of the required params is missing';
if ( $_GET ) {
print_r($_GET);
}
?>
Now if there is a problem like you forgot to change the mysql connection userid and password, or your hosting company use a seperate database server so localhost is not right, you will be told about the error.
Related
[sample][1]
Good day,
I successfully fetched the values from the database **(db) for the select option using mysqli
but
the problem is whenever I try save it in the database it doesn't retrieve the value selected in select the option .
Can you please give any suggestions on how to deal with this.
<?php
$conn = new mysqli('localhost', 'root', '', 'db') ;
if (mysqli_connect_error()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
} else {
echo"db_connection.php RUNNING<br>";
}
/* CONNECTION IN DATABASE*/
/* WHILE LOOP FOR SELECT OPTION IN DATABASE*/
$result = $conn->query("select b_fname from tbl_client");
echo "<html>";
echo "<body>";
echo "<select name='id'>";
while ($row = $result->fetch_assoc()) {
unset($b_id, $b_fname);
$b_id = $row['b_id'];
$b_fname = $row['b_fname'];
echo '<form action ="dropdown_demo.php" method="POST" enctype="multipart/form-data" >';
echo '<option name="b_fname" value="/'.$b_fname.'/">'.$b_fname.'.'.$b_fname.'</option>';
echo " </form>";
}
echo '</select><input type="submit" name="add_drop" />';
echo "</body>";
echo "</html>";
/* SQL INSERT THE VALUE TO THE */
$sql = "INSERT INTO tbl_client (b_fname)VALUES ( '$b_fname' )";
if (mysqli_query($conn, $sql)) {
echo('<script>alert("Record Added Successfully!");</script>');
// header('Refresh: 1; URL= import_addnew-Copy.php');
error_reporting(0);
} else {
error_reporting(0);
}
mysqli_close($conn);
?>
I have made some changes in your code. pu the tag out side while loop etc...
Try the below code:
$conn = new mysqli('localhost', 'root', '', 'db') ;
if (mysqli_connect_error())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}else
{
echo"db_connection.php RUNNING<br>";
}
/* CONNECTION IN DATABASE*/
/* WHILE LOOP FOR SELECT OPTION IN DATABASE*/
$result = $conn->query("select b_fname from tbl_client");
echo "<html>";
echo "<body>";
echo '<form action ="dropdown_demo.php" method="POST" enctype="multipart/form-data" >';
echo '<select name="b_fname" >';
while ($row = $result->fetch_assoc()) {
unset($b_id, $b_fname);
$b_id = $row['b_id'];
$b_fname = $row['b_fname'];
echo '<option value="'.$b_fname.'">'.$b_fname.'.'.$b_fname.'</option>';
}
echo '</select><input type="submit" name="add_drop" />';
echo " </form>";
echo "</body>";
echo "</html>";
/* SQL INSERT THE VALUE TO THE */
if(isset($_POST['add_drop'])){
$b_fname = $_POST["b_fname"];
$sql = "INSERT INTO tbl_client (b_fname) VALUES ( '".$b_fname."' )";
if (mysqli_query($conn, $sql)) {
echo('<script>alert("Record Added Successfully!");</script>');
// header('Refresh: 1; URL= import_addnew-Copy.php');
error_reporting(0);
} else {
error_reporting(0);
}
}
mysqli_close($conn);
I am making a simple page to test a database connection. When I tried accessing it from my browser, it says:
Server error
The website encountered an error while retrieving http://localhost:8888/blah/blah/test.php. It may be down for maintenance or configured incorrectly.
Here are some suggestions:
Reload this webpage later. HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
All I am doing is connecting to a database and displaying the tables. Here is what I have so far as the PHP code:
<?php
// Get Variables
$dbname = $_GET["dbname"];
$dbusername = $_GET["dbusername"];
$dbpass = $_GET["dbpass"];
$dbhost = $_GET["dbhost"];
$connection = mysql_connect("$dbhost","$dbusername","$dbpass");
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
else
{
echo "Connected";
$dbcheck = mysql_select_db("$dbname");
if (!$dbcheck) {
echo mysql_error();
}else{
echo "<p>Successfully connected to the database '" . $database . "'</p>\n";
// Check tables
$sql = "SHOW TABLES FROM `$database`";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
echo "<p>Available tables:</p>\n";
echo "<pre>\n";
while ($row = mysql_fetch_row($result)) {
echo "{$row[0]}\n";
}
echo "</pre>\n";
} else {
echo "<p>The database '" . $database . "' contains no tables.</p>\n";
echo mysql_error();
}
}
// some code
mysql_close($con);
?>
My error in the WAMP Apache logs is:
[03-Feb-2013 22:47:37 UTC] PHP Parse error: syntax error, unexpected end of file in /Applications/MAMP/htdocs/coursemanager/default/verify1.php on line 52
What would a unexpected end of file be?
It means you forgot to close the last }
<?php
// Get Variables
$dbname = $_GET["dbname"];
$dbusername = $_GET["dbusername"];
$dbpass = $_GET["dbpass"];
$dbhost = $_GET["dbhost"];
$connection = mysql_connect("$dbhost","$dbusername","$dbpass");
if (!$connection) {
die('Could not connect: ' . mysql_error());
} else {
echo "Connected";
$dbcheck = mysql_select_db("$dbname");
if (!$dbcheck) {
echo mysql_error();
} else {
echo "<p>Successfully connected to the database '" . $database . "'</p>\n";
// Check tables
$sql = "SHOW TABLES FROM `$database`";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
echo "<p>Available tables:</p>\n";
echo "<pre>\n";
while ($row = mysql_fetch_row($result)) {
echo "{$row[0]}\n";
}
echo "</pre>\n";
} else {
echo "<p>The database '" . $database . "' contains no tables.</p>\n";
echo mysql_error();
}
}
// some code
mysql_close($con);
}
When I print my code it only prints the question and description of id = 1 but not the rest of the table.
here is my code.
Please show me how to print my entire table which has like 20 questions or so...and also please show me how to make it so that the questions stay on the browser (even when I refresh the page) because currently the data does not stay on the browser when i refresh the page.
Thanks So Much!
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
if( $result = $conn->query($query)) {
$fetch = $result->fetch_assoc();
echo "<p>{$fetch['question']}</p>";
echo "<p>{$fetch['description']}</p>";
} else {
echo "failed to fetch array";
}
}
?>
You need a for each loop:
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
if( $result = $conn->query($query)) {
$fetch = mysql_fetch_array($result, MYSQL_ASSOC);
foreach($fetch as $ques) {
echo "<p>" . $ques['question'] . "</p>";
echo "<p>" . $ques['description'] . "</p>";
}
} else {
echo "failed to fetch array";
}
}
?>
All I've done there is change:
$fetch = $result->fetch_assoc();
echo "<p>{$fetch['question']}</p>";
echo "<p>{$fetch['description']}</p>";
to:
$fetch = mysql_fetch_array($result, MYSQL_ASSOC);
foreach($fetch as $ques) {
echo "<p>" . $ques['question'] . "</p>";
echo "<p>" . $ques['description'] . "</p>";
}
fetch_assoc() — Fetch a result row as an associative array
so it gets only 1 row you need to loop through the rest of the rows check the examples reference from php docs
I want to fetch database value in html tag, is it possible ?? I can fetch database value in textbox using this code
$con = mysqli_connect("localhost", "root", "", "hct_db"); // Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$client_id = mysqli_real_escape_string($con, $_POST['client_id']);
$result = mysqli_query($con, "SELECT * FROM client where client_id = '" . $client_id . "'");
while ($row = mysqli_fetch_array($result)) {
<input class="form-control" name="client_id1" value="<?php echo( htmlspecialchars( $row['address'] ) ); ?>" />
Try this (it will fill your input boxes with the value)
<?php
$con = mysqli_connect("localhost", "root", "", "hct_db"); // Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$client_id = mysqli_real_escape_string($con, $_POST['client_id']);
$result = mysqli_query($con, "SELECT * FROM client where client_id = '" . $client_id . "'");
echo '<form action="" method="post">';//openning upthe
while ($row = mysqli_fetch_array($result)) {
echo '<p>'.( htmlspecialchars( $row['address'] ) ).'</p>';
}
Yes, you can do it. At first, retrieve your data using query and keep it an array and then echo it.
yes you can try this and pls try something before posting a question...
<?php
$con=mysqli_connect("localhost","root","","hct_db"); // Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} // escape variables for security
$client_id = mysqli_real_escape_string($con, $_POST['client_id']);
$result = mysqli_query($con,"SELECT * FROM client where client_id = '".$client_id."'");
while($row = mysqli_fetch_array($result)) { //this is my retrieve code, what to do next ? ?>
<? echo $row[0]; ?> // href tag
<h2><? echo $row[0]; ?></h2> //h tag
<p><? echo $row[0]; ?></p> //p tag
//and so on
<?}?>
comment for further querys...
I have written this PHP code and worked correctly... then I wanted a particular code segment to be worked as a function, but as soon I did this I didn't get the correct result... I'm confused what has gone wrong, can somebody please help me with this... Thanks a lot...
Here's the code gives me the error...
$arr1=array();
$date = date("D");
$link = mysql_connect ('localhost', 'root', '');
$db = mysql_select_db ('dayevent', $link);
function grabData($arr){ //works properly NOT as a function, but I want to make this code part act like a funciton
$i=0;
$sql = "SELECT event FROM events WHERE day = '$date'";
$sel = mysql_query($sql);
echo $sel; //this prints Resource id #3
if (mysql_num_rows($sel) > 0) { // but doesn't go into if block
while($row = mysql_fetch_array($sel)) {
echo $row['event'] . '<br />';
//storing DB query result in array
$arr[$i]=$row['event'];
$i=$i+1;
}
foreach($arr as $key => $value) {
echo $key . " " . $value . "<br />";
}
} else echo 'Nothing returned!'; //prints this instead of the correct result
}
grabData($arr1);
mysql_close();
Move this inside your function: $date = date("D");. The way it is now, $date is not defined. If you run with error_reporting(E_ALL) you would have caught it right away.
Test Below Code :
EDITED
$arr1=array();
$date = date("D");
$link = mysql_connect ('localhost', 'root', '');
$db = mysql_select_db ('dayevent', $link);
function grabData()
{
global $link,$date;
$arr = array();
$i=0;
$sql = "SELECT event FROM events WHERE day = '$date'";
$sel = mysql_query($sql,$link);
echo $sel; //this prints Resource id #3
if (mysql_num_rows($sel) > 0)
{
while($row = mysql_fetch_array($sel))
{
echo $row['event'] . '<br />';
$arr[$i]=$row['event'];
$i=$i+1;
}
foreach($arr as $key => $value)
{
echo $key . " " . $value . "<br>";
}
} else echo 'Nothing returned!'; //prints this instead of the correct result
return $arr;
}
print_r( grabData() );
mysql_close();