Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've been trying to insert a tuple into my relation, but it doesn't seem to work.
<?php
$link=mysql_connect ("connection", "name", "pw");
mysql_select_db('pizzaria');
$sql = "INSERT INTO pizza_bakery (Name, Address, Telenum)
VALUES ('Test', 'Test', '111')";
?>
The connection seems to go through ok, as I'm able to print the table out easily on my html page, why is this failing?
Your insert statement should look something like this:
$sql = "INSERT INTO pizza_bakery (Name, Address, Telenum)
VALUES ('Test', 'Test', 111)";
Reason: The last column (Telenum) is an integer datatype and the insert statement was treating it like a string.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am trying to execute the following query:
INSERT INTO table_timesheet (name, datein, dateout)
VALUES ('Rupert', 'NOW()', '')
WHERE NOT EXISTS (
SELECT name FROM table_listnames WHERE datein='NOW()'
);
But this returns an error.
Basically I don't want to insert a record if the 'datein' field of the record already exists in another record - how to check if the new datein is unique?
And how can i insert in the same way the date out on the same row something like an update the datein row?
This INSERT query will do exactly what you want:
INSERT INTO table_timesheet (name, datein, dateout)
SELECT 'Rupert', NOW(), null
FROM DUAL
WHERE NOT EXISTS (
SELECT * FROM table_listnames WHERE datein=NOW()
);
Please see it here. But a proper solution would be to set a table constraint. How are table_timesheet and table_listnames related? I would use a unique constraint on the datein column, but this depends on what are your requirements.
first check this query (using MYSQLI or PDO ) if this query return false than you insert record
IF... this return false
SELECT name FROM table_listnames WHERE datein='NOW()'
FALSE
INSERT INTO table_timesheet (name, datein, dateout)VALUES ('Rupert', 'NOW()', '')
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a text box with an extension of thousands.
In the text box the user enters only "7" for 7000 and "10" for 10000 and "75" for 75000. The values of 7000, 10000 has to be stored in our database. How can we do this?
As of Initial step, I have created a normal database connection to store values in the database. My databse will store only values in the format of 7,10,75 as user entered but not of multiplication*1000
But I would like to store it in the form of thousands. Could you let me know how to code this in the database?
Thanks
Multiply it by 1000 while entering.
eq
<?php
$number=intval($_POST["number"])*1000;
$sql="insert into database.table values($number)";
//query to database
?>
You can do simple arithmetic with SQL. When you are inserting, use a query something like this:
INSERT INTO table (thous_val) VALUES (1000 * ?)
where ? is the value from your form.
Upon retrieval do something like this. Use the integer DIV operator or you'll get numbers after the decimal place.
SELECT (thous_val DIV 1000) val
FROM table
You can use CONCATENATION while inserting values to table.
Like...
$sql = "INSERT INTO YourTable (col1, col2, price) VALUES ('val1', 'val2', '".intval($_POST["price"])*1000."')";
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I need to insert image row from table 'users2' to 'allbets'. But suddenlly this code don't working and I don't know why.. What is wrong with this?
$q2 = $pdo->prepare('INSERT INTO allbets (image) SELECT users2.image FROM users2 WHERE username = ?');
$q2->bindValue(1, $_SESSION['name']);
$q2 -> execute();
This code did not suddenly stop working, it never could have worked with its present query syntax. Change the query to this -
$q2 = $pdo -> prepare('INSERT INTO allbets (user, bet, komanda, teams, cof, data, image) VALUES ($user, $bet, $komanda, $teams, $cof, $data, (SELECT `users2`.`image` FROM `users2` WHERE `username` = ?));
Do yourself a service and error checking to your PHP code and to your PDO. This will let you know where to look when errors occur.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Have no idea why it won't insert
$name = 'tom';
$email = 'swag';
$number = 123;
mysqli_query($connection, "INSERT INTO `table` (`name`, `email`,
`number`) VALUES ('$name', '$email', '$number')");
This is just a test to see why it won't insert, i have set the connection up and there are no errors being thrown.
According to the documentation for mysqli_query(), it returns FALSE on failure and TRUE on successful inserts.
So first, check its return value to see if it thinks it failed or succeeded. It probably thinks it failed, in which case, use mysqli_error() to get a string description of the error.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have a database called mkmatchmanager and it has a table called warviewer as you can see below.
You can see the structure of the table here. I am trying to add a row in this table:
$a= mysqli_connect("localhost","username","password","my_mkmatchmanager");
if (mysqli_connect_errno()) {
echo "Failed to connect";
}
mysqli_query($a, "INSERT INTO `my_mkmatchmanager`.`warviewer` (`clan1`, `score1`, `clan2`, `score2`, `table`) VALUES ('test3', '330', 'test4', '241', 'www.mylink.com'");
mysqli_close($a);
This code is included in a file called salva.php. This code by the way doens't work because the query doesn't add the data in the table. Do you have any idea?
Put your double quotes after the parentheses...instead of this:
mysqli_query($a, "INSERT INTO `my_mkmatchmanager`.`warviewer` (`clan1`, `score1`, `clan2`, `score2`, `table`) VALUES ('test3', '330', 'test4', '241', 'www.mylink.com'");
it should be this:
mysqli_query($a, "INSERT INTO `my_mkmatchmanager`.`warviewer` (`clan1`, `score1`, `clan2`, `score2`, `table`) VALUES ('test3', '330', 'test4', '241', 'www.mylink.com')");
should be
mysqli_query($a, "INSERT INTO `my_mkmatchmanager`.`warviewer` (`clan1`, `score1`, `clan2`, `score2`, `table`) VALUES ('test3', '330', 'test4', '241', 'www.mylink.com')");