update table in sql not working - php

I am tring to update data of private_tutor Table. But its not working .
But when I write the sql command
(UPDATE private_tutor SET name='private' , contact_number='0000' ,
address='dw', experience='s',
qualification='dwd' , age='dwd', about='dwd'
WHERE id=1)
in terminal it updates , table.
What may be the reason , I am giving the same query through php. why its not working ?
output
UPDATE private_tutor SET name='private' , contact_number='0000' , address='dw', experience='s', qualification='dwd' , age='dwd', about='dwd' WHERE id=1
There is some problem in adding record
update.php
<?php //start php tag
//include connect.php page for database connection
include('connect.php');
include('session.php');
//if submit is not blanked i.e. it is clicked.
Echo $_GET['profile-name'].$_GET['profile-contact'].$_GET['address'].$_GET['experience'].$_GET['qualification'].$_GET['age'].$_GET['about'].$_GET['id'];
If(isset($_GET['submit']))
{
$sql="UPDATE private_tutor
SET name='".$_GET['profile-name']."' ,
contact_number='".$_GET['profile-contact']."' ,
address='".$_GET['address']."',
experience='".$_GET['experience']."',
qualification='".$_GET['qualification']."' ,
age='".$_GET['age']."',
about='".$_GET['about']."'
WHERE id=".$_GET['id']." ";
echo "</br>".$sql."</br>";
$res=$conn->query($sql);
If($res)
{
header('Location:private-tutor-profile.php');
}
Else
{
Echo "There is some problem in adding record";
}
}
?>

Instead of outputting your own error message which tells you next to nothing about the problem, output the error message from the database connection.
So instead of this line
Echo "There is some problem in adding record";
Do this instead:
If $conn is PDO
echo print_r( $conn->error_info );
If $conn is MYSQLI_
echo $conn->error;
But quite possibly, your connection has failed and you do not show us that code!! And I assume you are not looking for error messages there either.

Related

echo doesnt work after pg_query

I'm currently trying to retrieve information from my database and display this data with a php file but it doesnt work as intended. Here is my code:
<?php
require("dbconnect.php");
if(!$db){
echo "Error: Unable to open database";
} else {
echo "Opened database successfully";
}
$result = pq_query($db, 'SELECT * FROM example');
if (!$result) {
echo "Error: Cant access table";
exit;
}
else {
echo "Everything works fine";
}
pg_close($db);
?>
Note: dbconnect.php opens the connection to the database with pg_connect() and saves this to $db.
I expected that it displays Opened database successfully Error: Cant access table because I havent created a table example yet. But I only get Opened database successfully. So I added echo "Test"; before pg_close($db); and expected that it displays Opened database successfully Test but no, it only shows Opened database successfully.
I then tried to create a new table with php so I added
pg_query($db, 'DROP TABLE IF EXISTS example');
pg_query($db, 'CREATE TABLE example (col char(1))');
before $result = pq_query($db, 'SELECT * FROM example');. I connected with ssh to the server after this and checked with psql if the table exists and it did, so the connection should work properly. But it still only shows me Opened database successfully and I expected Opened database successfully Everything works fine. I really dont know why every echo after $result = pq_query($db, 'SELECT * FROM example'); doesnt work. Can someone explain to me whats wrong?
Change pq_query for pg_query.
Note you have a Q instead of a G.

PHP Connection To Azure-SQL Database

In the below code, I try to connect to an azure-sql database with PHP, and in the first part of the if, I write out if the connection failed, which I don't receive. In the next else, I write out if the connection passed, which I get the message on ("Connection succeeded"). This appears to connect correctly; however, I do get an output of 0 even though it clearly hits this part of the else. I can't tell from research if 0 means success, but if it the connection failed, wouldn't it hit the first part of the if?
The problem is that this code isn't outputting the array column added to the string, so it appears that while it's registering as succeeding, it's actually failing, or something else is wrong, like the syntax.
<?php
/// Test variable
$writeOutResult = "Result: ";
/// VARIABLES NOT INCLUDED
/// Connect
$connInfo = array("Database"=>$azureDB
, "UID"=>$azureUser
, "PWD"=>$azurePass
, "MultipleActiveResultSets"=>true
);
$conn = sqlsrv_connect($azureServer,$connInfo);
/// Test connection
if($conn === false)
{
//FatalError("Server unavailable.");
$writeOutResult = "Connection failed.";
}
else
{
echo "Connection succeeded";
$get = sqlsrv_query($conn,$query);
while ($row = sqlsrv_fetch_array($get, SQLSRV_FETCH_ASSOC))
{
$writeOutResult += $row["Column"];
}
}
?>
<html>
<head><title></title></head>
<body>
<p>Output:</p>
<?php
echo $writeOutResult;
?>
</body>
</html>
It seems that there is no obvious error on PHP. I tested on my side and which worked fine.
It could be the case of following causes, you can check one by one.
Please check the SQL query stmt, whether it's correct, you can query it in SSMS for verification.
Check the value and data type. Whether it can be plus directly.
Any further concern, please feel free to let me know.

mysql php displays results but not inserting into database

I stripped down query for only one insert
<?php
session_start();
include 'cstring.php';
$title="";
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
$title=$_POST['title'];
$query=mysqli_query($con,"insert into blogpages(blogpagetitle) values('".$title."')");
if($query){
$bloga="sucessfully added a new blog";
echo $bloga;
}
else {
echo mysqli_error($con); // if using mysqli do not use mysql in between
}
}
mysqli_close($con);
?>
is there something wong in this code that it doesnt insert into mysql
table structure
1.bpid int(50)--------------null-no default-none autoincrement
2.blogpagetitle------------varchar(255) utf16_general_ci
3.datemade-------------timestamp current time stamp
4.blogpagedescription---------text utf16_general_ci
5.blogbody----------------longtext utf16_general_ci
6.blogpageextended------------ text utf16_general_ci
TIP
Sanitize variables, Use mysqli_real_escape_string()
When you are not able to debug your code, echo every possible stuff and die the rest code.
For example here, echo if there is error in DB connection, echo the query to see if it is correct, echo the result of query execution, echo if there is some error!
You should be using echo mysqli_error($con) to get the error message rather than mysql_error().

PHP cannot find tables even after successfully connecting to the database

The below code re-creates the issue I can't get around. I just can't seem to figure out where the problem lies - in the code? MySQL settings? or somewhere else? Any pointers in the right direction will be appreciated.
<html>
<head></head>
<body>
<?php
$db_name = "UserDB";
$open = mysql_connect("localhost", "root", "");
if($open)
echo "1. Successfully connected to MySQL";
echo "</br>";
$db = mysql_select_db($db_name, $open);
if($db)
echo "2. Successfully selected {$db_name} database";
echo "</br>";
$sql = "SHOW TABLES FROM `{$db_name}`";
$result = mysql_query($sql);
$print = mysql_num_rows($result);
if($result)
echo "3. {$print} tables found in {$db_name}";
?>
</body>
</html>
Here's my output:
1. Successfully connected to MySQL
2. Successfully selected UserDB database
3. 0 tables found in UserDB
The problem lies in line 3 of the output. It says "0" tables, which is incorrect. I have created "3" InnoDB tables in the selected DB. If I copy/paste and run the same SHOW TABLES query in phpmyadmin, it runs perfectly.
Any idea what is going on here??
Try using the wrapper function mysql_list_tables. I found it impossible once to use the SHOW TABLES due to some weird permissions definition, though I could use mysql_list_tables.

PHP SQL Truncate

I'm having a problem trying to truncate the 'requestID' field from my requests table.
This is my code.
<?php
include 'mysql_connect.php';
USE fypmysqldb;
TRUNCATE TABLE requestID;
echo "Request ID table has been truncated";
?>
I'm using server side scripting so no idea what error is coming back.
Anyone got an idea?
You aren't executing queries, you're just putting SQL code inside PHP which is invalid. This assumes you are using the mysql_*() api (which I kind of suspect after viewing one of your earlier questions), but can be adjusted if you are using MySQLi or PDO.
// Assuming a successful connection was made in this inclusion:
include 'mysql_connect.php';
// Select the database
mysql_select_db('fypmysqldb');
// Execute the query.
$result = mysql_query('TRUNCATE TABLE requestID');
if ($result) {
echo "Request ID table has been truncated";
}
else echo "Something went wrong: " . mysql_error();
Take a look at the function mysql_query which performs the query execution. The code to execute a query should look something like this.
$link = mysql_connect('host', 'username', 'password') or die(mysql_error());
mysql_select_db("fypmysqldb", $link) or die(mysql_error());
mysql_query("TRUNCATE TABLE requestID", $link) or die(mysql_error());
mysql_close($link);

Categories