here, i try to insert some data from my form, but there is always not inserted and i dont know where is the error, hope anyone tell me :)
my insert code
$sql="INSERT INTO $tbl_name (buyername, buyerjob, buyercompany, buyeraddress, buyercity, buyerpost, buyerphone, buyerfax, buyeremail, buyerweb, buyernob, buyertoo, buyerhonor)
`VALUES ('$name', '$job', '$company', '$address', '$city', '$post', '$phone', '$fax', '$email', '$web', '$nob', '$too', '$bhonor')";
is there any error? hope anyone help
Error Message
#1452 - Cannot add or update a child row: a foreign key constraint fails
(reg_db.buyer_db, CONSTRAINT buyer_db_ibfk_1 FOREIGN KEY (spouse_id) REFERENCES
spouse_db (spouse_id))
You don't set a value of spouse_id field,
but error message says that it is mandatory and must be set to a value present in spouse_db table.
Related
I don't know how to replace some data if a "user" already exists.
I've tried ON DUPLICATE KEY UPDATE but I came to realize that this will probably not work. Because the only value that isn't updated is 'user' in my code but the other 3 values are constantly updated every 5 minutes.
INSERT INTO online ( `user`, `bot`, `world`, `status` ) VALUES ('$User', '$Name', '$World', '$status')
ON DUPLICATE KEY UPDATE bot = VALUES ('$Name'), world = VALUES ('$World'), status = VALUES ('$status')
The idea is if, for example, user "bob" already exists update his other 3 values bot, world, status, instead of creating a new line and so on.
Edit: this is how I have it setup in Mysql
The argument to VALUES() should be the name of a column, not a string. You put the name of the column that you would have inserted into.
INSERT INTO online ( `user`, `bot`, `world`, `status` ) VALUES ('$User', '$Name', '$World', '$status')
ON DUPLICATE KEY UPDATE bot = VALUES (bot), world = VALUES (world), status = VALUES (status)
i am trying to create a user account and i got this error message.
A Database Error Occurred
Error Number: 1048
Column 'group_id' cannot be null
INSERT INTO `bitauth_users` (`username`, `password`, `active`, `activation_code`, `group_id`, `password_last_set`) VALUES ('FAAN', '0884e4e5928d672b182a37b885b5e6ec', 1, '7c6ef5bd05fd2d189df64da76e76017ecec7d233', NULL, '2016-06-23 04:07:08')
Filename: C:\inetpub\vhosts\vidoplus.com\httpdocs\system\database\DB_driver.php
Line Number: 330
Please what do i need to do to fix this. i am new at this. Thanks
As group_id itself suggest that it's a primary key of some table (I think its will be groups table) and treated as a foreign-key in your bitauth_users table.
So you can not pass NULL to it.
You have to pass some value which is related to the table where it is primary key.
Note:- please provide some real value (don't provide empty or 0 etc because at the time of fetching record you will face problem)
Form your error message, you need to give value of group_id (not null value). You can try this:
INSERT INTO bitauth_users (username, password, active, activation_code, group_id, password_last_set) VALUES ('FAAN', '0884e4e5928d672b182a37b885b5e6ec', 1, '7c6ef5bd05fd2d189df64da76e76017ecec7d233', 1, '2016-06-23 04:07:08');
Hope it will help ;)
group_id is not null field ... so if you want to store NULL value then make it nullable other wise you can store 0 or '' blank value.
.
INSERT INTO `bitauth_users` (`username`, `password`, `active`, `activation_code`, `group_id`, `password_last_set`) VALUES ('FAAN', '0884e4e5928d672b182a37b885b5e6ec', 1, '7c6ef5bd05fd2d189df64da76e76017ecec7d233', '', '2016-06-23 04:07:08')
In your sql,tha VALUES part,the group_id is setted to NULL.The error msg is "Column 'group_id' cannot be null".So you can convert NULL to zero.
The sql can be like this:
INSERT INTO `bitauth_users`
(`username`,`password`, `active`, `activation_code`, `group_id`,`password_last_set`)
VALUES
('FAAN', '0884e4e5928d672b182a37b885b5e6ec', 1, '7c6ef5bd05fd2d189df64da76e76017ecec7d233',
0, '2016-06-23 04:07:08')
Hope this can help you!
Everyone!
I am working on application using php and mysql. Basically, initially, I am inserting the new data entries using html form into the database where store# is my primary key. For now I can not update the existing store# (as its my primary key) and get a message saying "Duplicate entry for store 967 (example)".
I want to update the "store" table if entery exists. Here is my code posted below, but I am getting another error message
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '['967'],address=['500 kipling avenue 1'],dsm_name=['n/a'],phone=['416-967-' at line 1
I am not sure if I am using the if conditional at right spot.
**$sql = "INSERT INTO `stores`(`store_num`, `address`, `dsm_name`, `phone`, `router_type`, `high_speed_pri`, `dsl_log`, `dsl_pass`, `secondary_conn`, `sec_dsl`, `sec_pass`) VALUES ('$store' , '$address', '$dsm', '$phone', '$router', '$highspeedpr', '$dsllog', '$dslpas', '$secondary_conn' , '$secdsl' , '$sec_pass')";
$mysqli_query = "SELECT * from 'stores' WHERE $store = 'store_num'";
if ($mysqli_query == TRUE){
$sql = "UPDATE `stores` SET `store_num`=['$store'],`address`=['$address'],`dsm_name`=['$dsm'],`phone`=['$phone'],`router_type`=['$router'],`high_speed_pri`=['$highspeedpr'],`dsl_log`=['$dsllog'],`dsl_pass`=['$dslpas'],`secondary_conn`=['$secondary_conn'],`sec_dsl`=['$secdsl'],`sec_pass`=['$sec_pass'] WHERE 1";
}
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>**
Replace instead of Insert
Since your update statement includes all the same fields as Insert, you can simply use a REPLACE Statement. As stated on the linked documentation:
REPLACE works exactly like INSERT, except that if an old row in the
table has the same value as a new row for a PRIMARY KEY or a UNIQUE
index, the old row is deleted before the new row is inserted. See
Section 13.2.5, “INSERT Syntax”.
So, changing the code to the following should work:
$sql = "REPLACE INTO `stores`(`store_num`, `address`, `dsm_name`, `phone`, `router_type`, `high_speed_pri`, `dsl_log`, `dsl_pass`, `secondary_conn`, `sec_dsl`, `sec_pass`) VALUES ('$store' , '$address', '$dsm', '$phone', '$router', '$highspeedpr', '$dsllog', '$dslpas', '$secondary_conn' , '$secdsl' , '$sec_pass')";
Error Reason
Your problem is with the syntax in the update statement. What is store_num, is it a number or a string?
You should change your syntax to not include the square brackets in the actual mysql query.
If $Store is Number:
=['$store'], to =$store
If $Store is Text:
=['$store'], to ='$store'
Final Recommendation
Even better though will be use prepared statements which are also secure and avoid against SQL injection attacks.
You can do this logic with a single query, using on duplicate key update. First, you have to define store_num as a unique key, if it is not already a unique or primary key:
CREATE UNIQUE INDEX idx_stored_storenum on stores(store_num);
Then use this insert:
INSERT INTO `stores`(`store_num`, `address`, `dsm_name`, `phone`, `router_type`, `high_speed_pri`,
`dsl_log`, `dsl_pass`, `secondary_conn`, `sec_dsl`, `sec_pass`
)
VALUES ('$store' , '$address', '$dsm', '$phone', '$router', '$highspeedpr',
'$dsllog', '$dslpas', '$secondary_conn' , '$secdsl' , '$sec_pass')
ON DUPLICATE KEY UPDATE address = values (address),
dsm_name = values(dsm_name),
. . .
sec_pass = values(sec_pass);
Your particular problem is the square braces, which MySQL doesn't recognize.
This is query where i want to insert new data to database. Before this id is auto by using old system and now i make a new form by php. This is my query and now i want make auto increment id.How to make a auto increment id.Please Help me if anyone know.
$sql="INSERT INTO $tbl_name(id, name, icno, email, applyIntake_id, modeOfStudy_id,
choice1_id, dob, nationalityType, gender_id, race_id, highestEducation_id,
address1, address2, postcode, city, state_id, permanent_address1,
permanent_address2, permanent_postcode, permanent_city, permanent_state,
telephoneNo, mobileNo)
VALUES('$name', '$icno', '$email', '$id', '$applyIntake_id', '$modeOfStudy_id',
'$choice1_id', '$dob', '$nationalityType', '$gender_id', '$race_id',
'$highestEducation_id', '$address1', '$address2', '$postcode', '$city',
'$state_id', '$permanent_address1', '$permanent_address2',
'$permanent_postcode', '$permanent_city', '$permanent_state',
'$telephoneNo', '$mobileNo')";
$result=mysql_query($sql);
Try this for MYSQL DB..
ALTER TABLE
`TABLE_SCHEMA`.`TABLE_NAME`
CHANGE COLUMN
`ID` `ID` INT(6) NOT NULL
AUTO_INCREMENT;
Ok, one thing first: you should escape your parameters! See here.
To solve your problem: you can define a field in the database as auto_increment, what will automatically increment the id if left empty.
Amir you have 3 errors in above insert.
The columns count match but you try to insert into #tbl_name(email) value of $id
Please set the id field as autoincrement the answer is in above posts.
I think when preparing PHP $sql variable you have to use "." as string operator to get proper SQL INSERT statement with data from your variables - currently the below INSERT will insert instead of your name variable $name as string into DB.
Exmaple :
$name = 'Tom';
$email = 'tom#o2.pl'
$sql = "insert into table(name,email) VALUES (".$name.",".$email.");";
Then use following insert but apply the example of string join above - it should work:
$sql="INSERT INTO $tbl_name(name, icno, email, applyIntake_id, modeOfStudy_id,
choice1_id, dob, nationalityType, gender_id, race_id, highestEducation_id,
address1, address2, postcode, city, state_id, permanent_address1,
permanent_address2, permanent_postcode, permanent_city, permanent_state,
telephoneNo, mobileNo)
VALUES('$name', '$icno', '$email', '$applyIntake_id', '$modeOfStudy_id',
'$choice1_id', '$dob', '$nationalityType', '$gender_id', '$race_id',
'$highestEducation_id', '$address1', '$address2', '$postcode', '$city',
'$state_id', '$permanent_address1', '$permanent_address2',
'$permanent_postcode', '$permanent_city', '$permanent_state',
'$telephoneNo', '$mobileNo')";
$result=mysql_query($sql);
ALTER TABLE document MODIFY COLUMN document_id INT AUTO_INCREMENT;
There are a couple of reasons that your SQL might not work. First, you must re-specify the data type (INT in this case). Also, the column you are trying to alter must be indexed (it does not have to be the primary key, but usually that is what you would want). Furthermore, there can only be one AUTO_INCREMENT column for each table. So, you may wish to run the following SQL (if your column is not indexed):
ALTER TABLE document MODIFY document_id INT AUTO_INCREMENT PRIMARY KEY;
You can find more information in the MySQL documentation: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html for the modify column syntax and http://dev.mysql.com/doc/refman/5.1/en/create-table.html for more information about specifying columns.
I use:
INSERT INTO `rating` (`name`, `user`, `rating`, `section`, `ip`)
VALUES ('$name', '{$_SESSION['user']}', '$rate', '$section',
'{$_SERVER['REMOTE_ADDR']}');";
I would like to add an if condition in the IF statement so that.
IF SELECT ip from rating
where ip={$_SERVER['REMOTE_ADDR']} AND section=$section AND name=$name
then update ELSE INSERT new row
is it doable or I better code it in php ?
thank you very much
P.s: I know how to do it with php, I want to learn it with MySQL.
Also i require that all 3 name,section,ip matchs not only ip
Assuming you have a unique constraint (UNIQUE index or PRIMARY KEY) on ip, section and name, you can use this syntax:
INSERT INTO `rating` (`name`, `user`, `rating`, `section`, `ip`)
VALUES ('$name', '{$_SESSION['user']}', '$rate', '$section', '{$_SERVER['REMOTE_ADDR']}')
ON DUPLICATE KEY UPDATE user = VALUES(user), rating = VALUES(rating);
To expand on Eric's excellent answer.
To add a unique constraint on each of the columns ip, name, section run the following code on the database
ALTER TABLE `test`.`rating`
ADD UNIQUE INDEX `name`(`name`),
ADD UNIQUE INDEX `section`(`section`),
ADD UNIQUE INDEX `ip`(`ip`);
To run a unique constraint on the combination of columns ip+name+section do:
ALTER TABLE `test`.`rating`
ADD UNIQUE INDEX `name_section_ip`(`name`, `section`, `ip`);
The last thing is probably what you want.
One last thing
I'm not 100% sure, but this usage of {$_SERVER['REMOTE_ADDR']} in the query does not look SQL-injection safe.
Consider changing it into:
$remote_adr = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$session = mysql_real_escape_string($_SESSION['user']);
$query = "INSERT INTO `rating` (`name`, `user`, `rating`, `section`, `ip`)
VALUES ('$name', '$session', '$rate', '$section','$remote_adr')";
Finally putting a ";" in a mysql_query() like so mysql_query("select * from x;"); does not work mysql_query() will only ever execute one query and will reject your ;.