Blank Field in Mysql After Uploading Data - php

I was uploading data from my android application to a PHP file then inserting it to Mysql database, the problem i am having that i buy a new hosting plan and when i configured everything in the new hosting, i try to upload data from the application it shows only blank fields in the table, i am sure that there's no problem in the PHP or the android code, cause it was working fine and great with the old hosting.. i tried to change the encoding but same issue.
Here's the PHP file:
<?php
$con = mysql_connect("HOST","USER","PASS");
if (!$con)
{
die('Could not Connect:'. mysql_error());
}
mysql_select_db("TABLE",$con);
mysql_query ("INSERT INTO table (rep_desc,dateT) VALUES ('".$_REQUEST['report_Desc']."','".$_REQUEST['Date_Time']."')");
mysql_close($con);
?>
Thanks in advance

If there is any auto_increment column in table. Better check if its auto_increment flag not got uncheck.
Please also check length of database fields and data that you actually trying to insert.
Name of all request variables, columns, tables should be in proper case if it is a UNIX system.

get all the tables by something like this query ...
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}\n";
}

Related

How to store <li>-item created by user server sided (mySQL)

I have a simple <ul> where you can add a list item with an input field:
$('#plannerform').submit(function(e) {
var val = $(this).find('#plannername').val();
$('ul.plannerlist:visible').append('<li>' + val + '</li>');
e.preventDefault();
});
My question is: Is it possible in a way to save this created list on a mySQL database on a webserver? I haven't got much experience in PHP or other languages for server sided storage. And if it's possible, can you tell me any links where it's explained how? I spent some time already with searching, but I didn't find anything because I simply don't know what to search after.
Here is a simple php script that I wrote to connect to my MySQL DB, and retrieve along with display each result in a given column.
Just replace:
Hostname with probably your local IP if MySQL is installed on your local machine.
Your MySQL username, probably root if you haven't created another user.
The password, which could be blank if you didn't create one.
Database with the name of your Database.
<?php
//Connect to DB
$conn = new mysqli("Hostname","Username","Password","Database");
//If the connection has errors
if ($conn->connect_error){
//Display the error
die("Connection failed because: " . $conn->connect_error);
}
//Otherwise the connection is good so lets create a sql query
$sql = "SELECT * FROM Database";
//Get the results of the query
$result = $conn->query($sql);
//If the results have rows
if($result->num_rows > 0){
//While there are more rows in the results
while($row = $result->fetch_assoc()) {
//Display in HTML paragraph form: Column1 = DataInColumn1
echo '<p> Column1 = ' . $row["Column1"] . ' </p>';
}//End While
}//End If
//Otherwise the query had no results
else{
echo '<p>No results found!</p>';
}
?>

Displaying records from a table with PHP

Sorry for the newbie question, but I am working on my first PHP script and I can't seem to make it work. I just want to display the records from a single MySQL table. I have been trying to do this for ages and it is not displaying anything except the first two echo statements, before it is supposed to pull out the data.
What am I doing wrong?
<?php
mysql_connect("localhost", "me", "mypass") or die(mysql_error());
echo "Connection to the server was successful!<br/>";
mysql_select_db("test") or die(mysql_error());
echo "Database was selected!<br/>";
$result = mysql_query("SELECT * FROM Customer");
while($row = mysql_fetch_assoc($result)){
echo "ID: ".$row['customer_id'].", Name:".$row['customer_name']
."<br/>";
}
?>
echo mysql_num_rows($result);
to know the number of rows returned by your query.
This error is because the table or the database you are trying to connect doesnt exits.
As #barmar suggests table names are case sensitive..
Please make sure that you are using the correct database and table ..THanx

tables not being created in database

Ok, I'm following a youtube guide on how to create a very simple blogging system using PHP/MySQL as I'd like to get to learn these 2 languages a bit more. I'm creating this in my localhost, permissions set-up correctly.
The problem is, when I go onto localhost/tables.php, it comes up as white screen which it's supposed to, but it's not creating the relevant tables within the database?
Here's the code I'm using:
mysql.php
<?php
mysql_connect('localhost','username','password'); //where localhost is the host, username is the relevant username and password is the relevant password.
mysql_select_db('database'); //where database is the chosen database in which to drop the tables.
?>
tables.php
<?php
include "mysql.php";
$table = "ENTRIES";
mysql_query ("CREATE TABLE IF NOT EXISTS `$table` (`ID` INT NOT NULL AUTO_INCREMENT , PRIMARY KEY ( `id` ) )");
mysql_query ("ALTER TABLE `$table` ADD `TITLE` TEXT NOT NULL");
mysql_query ("ALTER TABLE `$table` ADD `SUMMARY` TEXT NOT NULL");
mysql_query ("ALTER TABLE `$table` ADD `CONTENT` TEXT NOT NULL");
?>
Nothing is appearing in the error log which is frustrating and not helping me diagnose the problem.
Any help would be much appreciated! Thanks.
As you have no errors run this code to show if you have created created the table ENTRIES.
<?php
include "mysql.php";
$result = mysql_query("SHOW COLUMNS FROM `ENTRIES`");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
}
?>
NOTE As you are starting with MySQL you would be advised to use PDO in place of the deprecated mysql_.
Here is a good tutorial
EDIT
Following comments the following code lists databases and tables on server(Note uses deprecated mysql_ function).
Ensure that the proper parameters replace "XXX".
<?php
$host= "localhost";
$username="XXX";
$password="XXX";
$database="XXX";
$link = mysql_connect($host,$username,$password); //where localhost is the host, username is the relevant username and password is the relevant password.
$db_list = mysql_list_dbs($link);
while ($row = mysql_fetch_object($db_list)) {
echo $row->Database . "\n";
echo "<BR>";
}
echo "<BR>";
$result = mysql_list_tables($database);
$num_rows = mysql_num_rows($result);
for ($i = 0; $i < $num_rows; $i++) {
echo "Table: ", mysql_tablename($result, $i), "\n";
echo "<BR>";
}
?>
Nothing is appearing in the error log which is frustrating and not helping me diagnose the problem
So your first poblem is to find out why it's not logging any errors. BTW it would also be a good idea to inject some echo / print statements to find out where it's failing.
Forget about the MySQL stuff and try:
<?php
trigger_error("Testing", E_USER_WARNING);
?>
Even though (once you've got the error reporting sorted out) you should get an error logged it will likely only contain a limited amount of information. Any time you call a mysql function, be via mysql_, mysqli or PDO, you should poll the return value and handle any error - even if it's just to echo the value to the screen.
It's saying that I haven't selected a database
Possibly the user account you are connecting as does not have permission to access the database.

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.

issue when inserting data to database

My code doesn't insert any records to mysql. What is wrong? I am really confused.
I have designed a form and I want to read data from text box and send to the database.
<?php
if(isset($_post["tfname"]))
{
$id=$_post["tfid"];
$name=$_post["tfname"];
$family=$_post["tffamily"];
$mark=$_post["tfmark"];
$tel=$_post["tftell"];
$link=mysql_connect("localhost","root","");
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("university",$link);
$insert="insert into student (sid,sname,sfamily,smark,stel) values ($id,'$name','$family',$mark,$tel)";
mysql_query($insert,$link);
}
mysql_close($link);
?>
You'd better to put quotation mark for id, mark and tel after values in your query. Also as #Another Code said, you must use $_POST instead of $_post in your code. Try this and tell me the result:
<?php
if(isset($_POST["tfname"])) {
$id=$_POST["tfid"];
$name=$_POST["tfname"];
$family=$_POST["tffamily"];
$mark=$_POST["tfmark"];
$tel=$_POST["tftell"];
$link=mysql_connect("localhost","root","");
if (!$link) {
die('Could not connect: ' . mysql_error());
} else {
mysql_select_db("university",$link);
$insert="insert into student
(sid,sname,sfamily,smark,stel) values
('$id','$name','$family','$mark','$tel')";
mysql_query($insert,$link) or die (mysql_error());
mysql_close($link);
}
} else {
die('tfname did not send');
}
?>
Use mysql_query($insert,$link) or die (mysql_error()); to fetch the error message.
With the code you've provided it could almost be anything - to do some tests... have you echo'd something to confirm you are even getting the tfname in POST? Does it select the database fine? Do the fields $id, $mark, and $tel need single quotes around them as well? We need to know more about where the code is not working to provide more help but that snippet appears as though it should be running, in the interim, please use some echo's to narrow down your problem!
Try to run the generated sql query in the sql query browser. Get the query by "echo $insert" statement.
change the $insert to:
$insert="insert into student (sid,sname,sfamily,smark,stel) values ($id,'".$name."','".$family."','".$mark."','".$tel."')";
further set ini_set('display_errors',1) so that php displays the error messages as required.
Lastly, whenever doing a mysql query, try to use or die(mysql_error()) in the query so that if somethings wrong with mysql or syntax, we are aware.
$q = mysql_query($query) or die(mysql_error());

Categories