PHP MySQL INSERTING - php

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);
?>

Related

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!');

Mysql insert not working and not giving errors

i do not know why the following code will not work for inserting data into mysql.
if (!$link = mysql_connect('server', 'user', 'password')) {
echo '700';
exit;
}
if (!mysql_select_db('vendors', $link)) {
echo '701';
exit;
}
$sql2 = "INSERT INTO transactions (TransID, payment_status, last_name, first_name, payer_email, address_name, address_state, address_zip, address_country, verify_sign, payment_gross, ipn_track_id, business, reciver_email) VALUES ('kris', 'kris', 'kris', 'kris', 'kris','kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris')";
$result2 = mysql_query($sql2, $link);
What is wrong with the code?
php is giving no errors.
Please try not to use mysql_connect instead use mysqli_connect or PDO_MySQL read this
Also use die to find if there is any errors in your code
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
Otherwise(recommended way)-
Procedural style
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO Persons (firstname, lastname, email)
VALUES ('Happy', 'John', 'john#example.com')";
if (mysqli_query($conn, $sql)) {
echo "New Person created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
MySQLi Object-oriented style
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Persons (firstname, lastname, email)
VALUES ('Happy', 'John', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New Person created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
Try changing this
$result2 = mysql_query($sql2, $link);
Into this
$result2 = mysql_query($sql2, $link)or die(mysql_error());
You have to write the code like below to get the errors in your code
$result = mysql_query($sql2,$link) or die(mysql_error());
this or die(mysql_error()) will give you errors in query

INSERT statement not working mysql

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";

Simple php/mysql not working

I have the following in a php script.All I get is a blank page, no errors or nothing.
error_reporting(E_ALL);
ini_set("display_errors", 1);
$database = "mydatabase";
$con = mysql_connect("localhost", "admin", "password") or die(mysql_error());
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db = mysql_select_db($database);
if(!$db){
die('Could not connect: ' . mysql_error());
}
if(isset($_POST['id'])){
$userid = mysql_real_escape_string($_POST['id']);
echo($userid);
}
if(isset($_POST['name')){
$username = mysql_real_escape_string(htmlentities($_POST['name']));
echo($username);
}
$query = mysql_query("SELECT * FROM userinfo
WHERE userid ='$userid'")or die(mysql_error());
if(mysql_num_rows($query) > 0){
echo "yeah";
}else{
$query = mysql_query("INSERT INTO userinfo (username,userid)
VALUES ($username,$userid)")or die(mysql_error());
if(mysql_affected_rows($query)== 1){
echo "UPDATED";
}else{
echo "NOPE";
}
}
You should format your code better. Also you where missing a close ] bracket on this line, if (isset($_POST['Name')) {
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$database = "mydatabase";
$con = mysql_connect("localhost", "admin", "password") or die(mysql_error());
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db = mysql_select_db($database);
if(!$db)
{
die('Could not connect: ' . mysql_error());
}
if(isset($_POST['id']))
{
$userid = mysql_real_escape_string($_POST['id']);
echo($userid);
}
if(isset($_POST['name']))
{
$username = mysql_real_escape_string(htmlentities($_POST['name']));
echo($username);
}
$query = mysql_query("SELECT * FROM userinfo WHERE userid ='$userid'")or die(mysql_error());
if(mysql_num_rows($query) > 0)
{
echo "yeah";
}
else
{
$query = mysql_query("INSERT INTO userinfo (username,userid) VALUES ($username,$userid)")or die(mysql_error());
if(mysql_affected_rows($query)== 1)
{
echo "UPDATED";
}
else
{
echo "NOPE";
}
}
?>
You also have an error in your SQL:
INSERT INTO userinfo (username,userid)
VALUES ($username,$userid)
The values here should be quoted:
INSERT INTO userinfo (username,userid)
VALUES ('$username', '$userid')

Categories