INSERT statement not working mysql - php

When I process this data, the data is not being inserted into my database. This is my current database structure:
https://drive.google.com/file/d/0BzJ9StkJe55WaG1oaVhqcUJmSGc/edit?usp=sharing
I have no clue why it is not inserting. I am not getting an error message and I am seeing the successfully inserted data piece.
<head><title>Process Punch</title></head>
<?php
define('DB_NAME', 'name');
define('DB_USER', 'user');
define('DB_PASSWORD', 'pass');
define('DB_HOST', 'host');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' .mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}else{
$userid_value = $_POST['userid'];
$punchtype_value = $_POST['punchtype'];
$group_value = $_POST['group'];
$dept_value = $_POST['dept'];
$notes_value = $_POST['notes'];
$table = "tc_".$userid_value;
$date_value = date("Y-m-d h:i:s");
echo $table;
$sql = "INSERT INTO $table (punchtype, groupname, dept, notes) VALUES ('$punchtype_value', '$group_value', '$dept_value', '$notes_value')";
echo "Successfully inserted data";
}
?>

Execute the query with mysql_query(), concatenate the query
$sql = "INSERT INTO `".$table."` (`punchtype`, `groupname`, `dept`, `notes`) VALUES ('".$punchtype_value."', '".$group_value."', '".$dept_value."', '".$notes_value."')";
$qry = mysql_query($sql ,$link);
echo "Successfully inserted data";

Related

How to properly update a MySQL table in PHP

I'm trying to update a row in a table using PHP, although I'm this is not my first time, but the update function is not working.
This is the php code:
<?php
header("Access-Control-Allow-Origin: *");
$server = "localhost";
$username = "username";
$password = "dbpass";
$database = "dbname";
$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());
mysql_select_db($database, $con);
$q=mysqli_query("UPDATE `phonegap_login` SET `firstname` = 'Alhia' WHERE `reg_id` =50;");
if($q)
{
echo "success";
}
else
{
echo "failed";
}
?>
And by the way, if you try INSERT into, it will work:
$q=mysql_query("insert into `phonegap_login` (`reg_date`, `firstname`, `lastname`, `email`, `password`) values ('test', 'test', 'test', 'test', 'test')");
How do I fix it? the insert is working, updating a row is not.
I'm using a row with reg_id=50 for testing.
You're mixing mysqli_query and mysql_query. Only use mysqli_query. mysql_query is deprecated
$con = mysqli_connect($server, $username, $password) or die ("Could not connect: " . mysqli_error());
mysqli_select_db($database, $con);
$q=mysqli_query("UPDATE `phonegap_login` SET `firstname` = 'Alhia' WHERE `reg_id` =50;");
Or, Object Oriented:
$mysqli = new mysqli($server, $username, $password, $database);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$q = $mysqli->query("UPDATE `phonegap_login` SET `firstname` = 'Alhia' WHERE `reg_id` =50;");
Or, PDO:
$connectionString = 'mysql:host=' . $server . ';dbname='.$database;
try {
$db = new PDO($connectionString, $user, $pass);
} catch ( PDOException $e) {
echo "Failed to connect to Database: (" . $e->getCode() . ") " . $e->getMessage();
}
$q = $db->query("UPDATE `phonegap_login` SET `firstname` = 'Alhia' WHERE `reg_id` =50;");

mysql api submite towice

I tried many times to submit the form when it submitted it repeated the submission twice on the data. I don't understand why,please help me. and when I put the header location it doesn't work ever
here is the code
<?php
$name= $_POST['form_name'];
$mrn= $_POST['form_mrn'];
$mobile= $_POST['form_mobile'];
$link = mysql_connect('server', 'user', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('dbname');
if (!$db_selected) {
die('Could not select database: ' . mysql_error());
}
mysql_set_charset('utf8',$link);
$query = "INSERT INTO pharmacy ( name , mrn , mobile ) VALUES ('$name', '$mrn', '$mobile')";
$result = mysql_query($query);
header('Location: form.html');
$link->close();
?>
<?php
$name= $_POST['form_name'];
$mrn= $_POST['form_mrn'];
$mobile= $_POST['form_mobile'];
$link = mysql_connect('server', 'user', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('dbname');
if (!$db_selected) {
die('Could not select database: ' . mysql_error());
}
mysql_set_charset('utf8',$link);
//Just Put this code before inserting
if($name !=""){
$query = "INSERT INTO pharmacy ( name , mrn , mobile ) VALUES ('$name','$mrn', '$mobile')";
$result = mysql_query($query);
header('Location: form.html');
}
$link->close();
?>
Just check condition before insert. Let me know if facing same issue.

PHP Error: No database selected (Hostgator.com)

I have a problem with inserting data to MySQL with PHP.
When I run this code on my Hostgator hosting I get error like this:
No database selected
Here is my code:
$dbh= mysql_connect("localhost", $username, $password);
// or die ('I cannot connect to the database because: ' . mysql_error());
if(!$dbh)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("contafe_tipster", $dbh);
//$conn = mysqli_connect($servername, $username, $password, $dbname);
//Check connection
/*
if (!$conn) {
// Connection failed return 0
//die("Connection failed " . mysqli_connect_error());
echo "1";
}
*/
//Posted parameters
$pName = $_POST['name'];
$pCountry = $_POST['country'];
$pCity = $_POST['city'];
$pLocationX = $_POST['locationX'];
$pLocationY = $_POST['locationY'];
$pDescription = $_POST['description'];
$pMobileNumber = $_POST['mobile'];
$pOtherNumber = $_POST['phone'];
$pOpens = $_POST['opens'];
$pCloses = $_POST['closes'];
$add_DB = "INSERT INTO Places (Id, Name, Country, City, LocationX, LocationY, Description, Mobile, Phone, Opens, Closes)
VALUES(NULL, '$pName', '$pCountry', '$pCity', '$pLocationX', '$pLocationY', '$pDescription', '$pMobileNumber', '$pOtherNumber', '$pOpens', '$pCloses')";
if (mysql_query($add_DB, $dbh)) {
//if success return 1
echo "0";
}
else {
//if error return -1
//echo "2";
die('Error: ' . mysql_error());
}
mysql_close($dbh);
Are you sure that db name is correct?
Try this way of connection with db:
mysql_connect ($dbhost,$dblogin,$dbpass) or die ("Can't connect to database");
mysql_select_db($db) or die ('Wrong databse!');

Get notified by email when people fill my form

Okey so basically I have a form so if people are interested in getting a website of me so they can fill it out and it was sent directly to my database via a php script. My question is can I do something so that I also get the message with all the information on my email? You can check out my currently script down below.
<?php
define('DB_NAME', 'database');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'server');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$value = $_POST['ID'];
$value2 = $_POST['name'];
$value3 = $_POST['email'];
$value4 = $_POST['message'];
$sql = "INSERT INTO table (ID, name, email, message) VALUES ('$value', '$value2', '$value3', '$value4')";
if (!mysql_query($sql)){
die('Something went wrong: ' . mysql_error());
}
header("Location: http://example.com");
mysql_close();
?>

PHP MySQL INSERTING

I have a php with mysql that would insert some data to the database if there isn't the same information
<?php
$id=$_POST['id'];
$guildname=$_POST['guildname'];
$level=$_POST['level'];
$score=$_POST['score'];
$guildmaster=$_POST['guildmaster'];
$con = mysql_connect("localhost", "root", "");
if (!$con)
{die('Could not connect to mysql: ' . mysql_error());}
$mydb = mysql_select_db("gunbound");
if (!$mydb)
{die('Could not connect to database: ' . mysql_error());}
$dup = mysql_query("SELECT Id FROM guildrequest WHERE Id='".$_POST['id']."'");
if(mysql_num_rows($dup) >= 1){
echo '<b>You have already ask for guild request.</b>';
}
else
{
$dup2 = mysql_query("INSERT INTO guildrequest VALUES ('$id', '$guildname', '$level', '$score', '$guildmaster')");
}
Print "<center>You have requested to join the guild.</center>";
mysql_close($con);
?>
but its not adding the record to the database if there isn't a record equal
Nor executing this:
$dup2 = mysql_query("INSERT INTO guildrequest VALUES ('$id', '$guildname', '$level', '$score', '$guildmaster')");
even if the code:
if(mysql_num_rows($dup) >= 1){
says that he can do the action of inserting
please help me
Try this:
<?php
$id=$_POST['id'];
$guildname=$_POST['guildname'];
$level=$_POST['level'];
$score=$_POST['score'];
$guildmaster=$_POST['guildmaster'];
$con = mysql_connect("localhost", "root", "");
if (!$con)
{die('Could not connect to mysql: ' . mysql_error());}
$mydb = mysql_select_db("gunbound");
if (!$mydb)
{die('Could not connect to database: ' . mysql_error());}
$dup = mysql_query("SELECT Id FROM guildrequest WHERE Id='".$_POST['id']."'");
if(mysql_num_rows($dup) >= 1){
echo '<b>You have already ask for guild request.</b>';
}
else
{
$dup2 = mysql_query("INSERT INTO guildrequest VALUES ('$id', '$guildname', '$level', '$score', '$guildmaster')");
return $dup2;
}
Print "<center>You have requested to join the guild.</center>";
mysql_close($con);
?>
I have add return to your else statement. I will execute your $dub2 variable. You could if you want to leave the variable $dub2 out then you will intermediately execute your query. Another way would be to use mysql_execute() function.
This would be a MYSQLI equivalent:
<?php
$id=$_POST['id'];
$guildname=$_POST['guildname'];
$level=$_POST['level'];
$score=$_POST['score'];
$guildmaster=$_POST['guildmaster'];
$host = "hostname";
$user = "username";
$password = "password";
$database = "database";
$con = mysqli_connect($host, $user, $password, $database);
if (!$con)
{die('Could not connect to mysql: ' . mysql_error());}
$dup = "SELECT Id FROM guildrequest WHERE Id='".$_POST['id']."'";
mysqli_query($con, $dup);
if (!$dup)
{die('Could not connect to database: ' . mysql_error());}
if(mysqli_num_rows($dup) >= 1){
echo '<b>You have already ask for guild request.</b>';
}
else
{
$dup2 = "INSERT INTO guildrequest VALUES ('$id', '$guildname', '$level', '$score', '$guildmaster')";
mysqli_query($con, $dup2);
}
Print "<center>You have requested to join the guild.</center>";
mysqli_close($con);
?>

Categories