<?php
include('connection.php');
/* code for id goes here */
$q2= "select MAX(id) from usertable";
echo mysqli_query($sql,$q2);
$name=$_POST['name'];
$username=$_POST['usrname'];
$password=$_POST['psw'];
$dob=$_POST['date'];
$query="insert into usertable(name, username, password, dateofbirth) values('$name', '$username','$password','$dob')";
if(mysqli_query($sql,$query))
{
echo "Registered successfully";
}
?>
This is my insert command to register a user in my database. However the database contains a column named id which is the primary key. How do I fetch the id before executing the insert query so that it fetches the last id and increments it by 1 and inserts it in the db along with the other data. The id is numeric and I want the program to perform the operation itself rather than the user entering the data of the id. Please help.
You need to set your id field to auto incement and your database will do it automatically:
ALTER TABLE usertable MODIFY COLUMN id INT auto_increment;
You need to write a query
SELECT MAX(id) FROM usertable
and get the result from that and then increment that value by 1 and then set that value in id field.
I am trying to enter a whole table in itself changing the city variable but when I create a temp table to store data the insert statement to insert data back in original statement not working. Here is the code
<?php
if(isset($_POST['btnsub'])){
$city=$_POST['city'];
$query= $conn->query("create table from_php like menuinstant;
insert into from_php select * from menuinstant where city='Kota';
update from_php set id = replace(id,'Kota','.$city.');
update from_php set city = replace(city,'Kota','.$city.');
insert into menuinstant select * from from_php;
drop table from_php
");
echo "table created";
}
?>
The insert into menuinstant is not executing even the drop query after that is also working. Help me out.
almost all database/sql wrappers will only allow exactly ONE query per call. so your $conn->query([5queries]) should be five $conn->query([1stquery]); $conn->query([2ndquery]); ...
update1:
You should/could also check for errors:
$result = $conn->query('[Your query here]');
if($result === false) {
die(print_r($conn->errorInfo(),true));
}
update2: please read up on mysql injections. for example http://php.net/manual/en/security.database.sql-injection.php
I am working on a MySQL query to check duplicate values from table using MySQL and PHP. Let me to explain my table structure and conditions.
db_product_info
pro_id product_name product_code status
The above are my columns in db_product_info table.
conditions:
1-> When table has no value the data should entry in table.
2-> In same product name only same product code will be inserted for once/multiple time in table.If input will different code for same product name (for more than one entry) it should be checked and return the validation message.
3- Duplicate of product_code will be checked according to the product_name as explained above.means for same product_name only same product_code should entry multiple time but this product_code should not used for any other product_name .
Let me show my code below.
require_once("../include/dbconfig.php");
$result=array();
$fields=array("Product_name","status","product_code");
$tablename=PREFIX."product_info";
$values=array($_POST['product_name'],$_POST['status'],$_POST['product_code']);
$id=db_insert($tablename,$values,$fields);
if($id){
$result[0]["msg"]="Added successfully";
$result[0]['id']=$id;
echo json_encode($result);
}else{
header("HTTP/1.0 401 Unauthorized");
$result[0]["msg"]="Unable to add try again";
$result[0]["data"]=$values;
echo json_encode($result);
}
How can I resolve this problem?
category --> `cat_id`,`name`
sub_category --> `sub_cat_id`,`name`
category_subcategory_association --> `cat_sub_cat_id`,`cat_id`,`sub_cat_id`
order_master --> `order_id`,`cat_sub_cat_id`,`cat_id`,`sub_cat_id`,`order_name`
These are the database tables which iam having while adding the orders into order_master table if we give cat_id,sub_cat_id while inserting into the table it should check the whether these ids are present in the association table or not,if the id is present in association table then we should get that association id and should insert into the orders table.
How can we do this can anyone help me
Thanks in advance.
You just need to check if it exists, if not - insert it into the the db and fetch the newest id.
I.E:
$cat_name = ; // your code
$sub_cat_name =; // your code
$query = mysql_query("SELECT cat_id FROM `category` AS `c` WHERE name = '$cat_name'");
if(!mysql_num_rows($query)) {
mysql_query("INSERT INTO category (name) VALUES ('$cat_name')");
$cat_id = mysql_insert_id();
} else
$cat_id = mysql_fetch_array($query);
// now do the same for the sub category and then insert it into to order_master
this is a general code, you need to customize it into your framework's standards.
hope it helped
you implement the checks via a select statement:
http://dev.mysql.com/doc/refman/5.1/de/select.html
running multiple sql queries, eg. a mixture of select and update statements requires you some knowledge on:
http://dev.mysql.com/doc/refman/5.0/en/innodb-transaction-model.html
http://php.net/manual/de/mysqli.multi-query.php
I have database with a table called members, which stores data for my admin,client,technicians all in one.But now I want to break this table into three separate tables as admin table,client table and techs table.As I have now written queries based on the members table I want to keep the common fields which were used in queries in the members table as they are now.Therefore I reduced the members table only to have fields ID,Name,UserName,Password,Level,Category.Throughout the site it is recognized if the user is a admin,client,or technician by setting levels(which corresponds to Level field).
When inserting data I tried using this code.First insert the common fields to members and then depending on the level write separate queries specifying to which table should data be inserted.
if($_POST['Submit']){
if(mysql_fetch_array(mysql_query("SELECT * FROM `members` WHERE UN='$r[UN]' AND ID!='$r[ID]' "))) {
?><script language="javascript" type="text/javascript">alert('Username already exsits.Please enter another.');
</script>
<?php
}
else {
mysql_query("INSERT INTO members (Name,UN,Password,Level,Category) VALUES ('$r[Name]','$r[UN]','$r[PW]','$r[Level]','$r[Category]')");
$row=mysql_query("SELECT * FROM members WHERE Name='$r[Name]'") or die('query unsuccesful');
if ($row['Level']==1){
mysql_query("INSERT INTO client(Name,Mobile,Phone,Fax,Email,Address) VALUES ('$r[Name]','$r[Mobile]','$r[Phone]','$r[Fax]','$r[Email]','$r[Address]')");
}
elseif($row['Level']==2){
mysql_query("INSERT INTO techs (Name,,Category,Company,Price,Comments,Rate,Qualifications,Mobile,Phone,Fax,Email,Address) VALUES ('$r[Name]','$r[Category]','$r[Company]','$r[Price]','$r[Comments]','$r[Rate]','$r[Qualifications]','$r[Mobile]','$r[Phone]','$r[Fax]','$r[Email]','$r[Address]')");
}
else{
mysql_query("INSERT INTO admin (Name,Mobile,Phone,Fax,Email,Address) VALUES ('$r[Name]','$r[Mobile]','$r[Phone]','$r[Fax]','$r[Email]','$r[Address]')");
}
if(!$user){
$user=$r;
$_SESSION['user']=$r;
}
But data gets inserted only to members table and nothing goes for client,techs or admin.Any idea how I can mend this please
You are not fetching data on the 2nd select.
$row=mysql_query("SELECT * FROM members WHERE Name='$r[Name]'") or die('query unsuccesful');
if(mysql_fetch_array($row)) {... }