So first of all I'm trying to populate a database with this PHP script. The error I am receiving is - "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 '' at line 1" I understand what the error is I just cannot seem to find it.
<?php
include("server_connect.php");
//select the db
mysql_select_db("moviedatabase");
//populate titles
$insert = "INSERT INTO tblMovieTitle (movie_id, movie_title, movie_genre, movie_rating, movie_actor_fname, movie_actor_lname) " .
"VALUES (1, 'Inglorious Basterds', 'War', 8, 'Brad', 'Pitt'), " .
"(2, 'Zombieland', 'Comedy', 8, 'Jesse', 'Eisenberg'), " .
"(3, 'Cowboys and Aliens', 'Action', 6, 'Olivia', 'Wilde'), " .
"(4, 'Contagion', 'Drama', 7, 'Matt', 'Damon')";
$results = mysql_query($insert) or die (mysql_error());
//populate genre
$genre = "INSERT INTO tblMovieGenre (movie_genre_ID, movie_genre) " .
"VALUES (100, 'Action'), " .
"(101, 'Horror'), " .
"(102, 'Comedy'), " .
"(103, 'War'), " .
"(104, 'Drama'), " .
"(105, 'Kids'), " .
"(106, 'Sci-Fi'), " .
"(107, 'Action'), " ;
$results = mysql_query($genre) or die (mysql_error());
//populate movie rating
$rating = "INSERT INTO tblMovieRating (movie_rating_ID, movie_rating ) " .
"VALUES (1001, 1), " .
"(1002, 2), " .
"(1003, 3), " .
"(1004, 4), " .
"(1005, 5), " .
"(1006, 6), " .
"(1007, 7), " .
"(1008, 8), " .
"(1009, 9), " .
"(1010, 10)";
$results = mysql_query($rating) or die (mysql_error());
//check if database is populated
echo "Database populated";
?>
Yeah so I really can't figure out where the error is coming from, anyone? Thanks beforehand for the help.
"(107, 'Action'), " ;
You don't need the extra comma ,
Related
I already saw other post related to this question, but they did not solve my problem. I want to automatically go to next rows and want to insert that row in the table. I am transfering sample of data from another database. I want to insert large set of rows... Right now I am getting first row repeatedly in all rows. I need to go to to next rows automoatically
Below is my sample code...
while ($row = mysql_fetch_array($query)) {
$sql = "INSERT INTO table_name (info1, info2, info3,...) "
."VALUES (" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . "),
(" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . "),
(" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . "),
(" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . ")";
}
This sql Query could help you. You can insert the select datas in one Query. See MySQL documentation: http://dev.mysql.com/doc/refman/5.7/en/insert-select.html
INSERT INTO table_name
(info1, info2, info3)
SELECT
source_table.info1,
source_table.info2,
source_table.info3,
FROM
source_table
WHERE
(WHERE CLAUSE)
do a separate insert for each selected rows:
while ($row = mysql_fetch_array($query)) {
$sql = "INSERT INTO table_name (incremented_field, info1, info2, info3) "
."VALUES (NULL, " . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . ")";
mysql_query($sql);
}
Why is my code returning a 500 Internal Server error on the line $result = mysql_query("SELECT * FROM institutions"); Am I doing something horrifically wrong? All I am trying to do is count the number of rows in a MySQL table (called 'institutions') after I have just added a row to that table.
$institution_sql = "
INSERT INTO `institutions`
(`InstitutionName`, `HeaderPictureID`, `Description`, `DevicesInfo`, `DoingInfo`, `FacebookPage`, `Location`, `TwitterHandle`, `Website`, `CreatedAt`)
VALUES
(" . nz($_POST['TempInstitutionName']) . ", 74, 'N/A', 'N/A', 'N/A', 'N/A', 'On the Internet', 'N/A', 'N/A', NOW())
";
$mysqli->query($institution_sql);
if ($mysqli->errno) {
$dbreturn['status'] = "PASSWORD_FAILURE";
} else {
$dbreturn['status'] = "EXEC_SUCCESS";
$result = mysql_query("SELECT * FROM institutions");
$rows = mysql_num_rows($result);
echo "There are " . $rows . " rows in my table.";
$insert_sql = "
INSERT INTO `users`
(`Handle`, `Email`, `FirstName`, `LastName`, `InstitutionID`, `TempInstitutionName`, `TwitterHandle`, `ProfilePictureID`, `HeaderPictureID`, `AccountType`, `CreatedAt`)
VALUES
(" . nz($_POST['Handle']) . ", " . nz($_POST['Email']) . ", " . nz($_POST['FirstName']) . ", " . nz($_POST['LastName']) . ", $num_rows, " . nz($_POST['TempInstitutionName']) . ", " . nz($_POST['TwitterHandle']) . ", " . nz('75') . ", " . nz('74') . ", " . nz($_POST['AccountType']) . ",NOW())
";
$mysqli->query($insert_sql);
if ($mysqli->errno) {
$dbreturn['status'] = "EXEC_FAILURE";
} else {
$dbreturn['status'] = "EXEC_SUCCESS";
$insertid = $mysqli->insert_id;
$password_sql = "
INSERT INTO `passwords`
(`UserID`)
VALUES
('$insertid')
";
$mysqli->query($password_sql);
if ($mysqli->errno) {
$dbreturn['status'] = "PASSWORD_FAILURE";
} else {
$dbreturn['status'] = "EXEC_SUCCESS";
}
} //todo: use a transaction here
}
your problem is that you mixing MYSQLI with MYSQL
rewrite your code using mysqli
$result = $mysqli->query("SELECT * FROM institutions");
$rows = $result->num_rows ;
// and so on ...
you are connecting via mysqli and then you use mysql in your code.
$result = mysql_query("SELECT count(*) FROM institutions");
This will directly return the number of rows.
This link can detail you
http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html
Use
$result = $mysqli->query($institution_sql);
$result->num_rows;
Or for plain old mysql
$result = mysql_query($institution_sql);
mysql_num_rows($result);
Try this:
$result = mysql_query("SELECT count(*) FROM institutions");
MySQL documentation: http://dev.mysql.com/doc/refman/5.0/en/select.html
Also this: http://www.w3schools.com/sql/sql_func_count.asp
SQL COUNT(*) Syntax
The COUNT(*) function returns the number of records in a table:
...also, that should be:
VALUES
('" . nz($_POST['TempInstitutionName']) . "', 74
Note the single quotes [unless the 'nz' function takes care of that].
I am trying to make a php calendar, as part of a project, and here I am executing some SQL with php:
mysql_select_db("waycov_dtm", $con);
$data="'Placeholder Text'";
$sql="INSERT INTO waycov_dtm.calDates (dateID, day, month, year)
VALUES (" . $dateID . ", " . $day . ", " . $month . ", " . $year . ");
INSERT INTO waycov_dtm.calData (dateID, data, memberID)
VALUES (" . $dateID . ", " . $data . ", 1);";
echo $sql;
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
When this executes, I get the following (the echoed sql and then the error):
INSERT INTO waycov_dtm.calDates (dateID, day, month, year) VALUES (7072012, 7, 07, 2012); INSERT INTO waycov_dtm.calData (dateID, data, memberID) VALUES (7072012, 'Placeholder Text', 1);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 'INSERT INTO waycov_dtm.calData (dateID, data, memberID) VALUES (7072012, 'Placeh' at line 3
If i take out any one insert statment, the other runs, but both together causes this??
Use it like this:
$sql="INSERT INTO waycov_dtm.calDates (dateID, day, month, year)
VALUES (" . $dateID . ", " . $day . ", " . $month . ", " . $year . ")";
$sql_second = "INSERT INTO waycov_dtm.calData (dateID, data, memberID)
VALUES (" . $dateID . ", " . $data . ", 1);";
if (!mysql_query($sql,$con) || !mysql_query($sql_second,$con))
{
die('Error: ' . mysql_error());
}
From the manual:
mysql_query() sends a unique query (multiple queries are not
supported)
The use of mysql_query is discouraged. Use MySQLi or PDO.
I have two databases same server same domain and same username and password. I need away to select from the main DB and then the second DB I need to be able to connect to it and insert data.
this is currently what I am trying but it is not working.
public function publish(){
$result = mysql_query("SELECT * FROM customer_detail WHERE approvedforsite = 2");
while($row = mysql_fetch_array($result))
{
echo $row['customer_id'] . " - " .$row['TradingName'] . " - " . $row['Phone'] . " - " . $row['Street'] . " - " . $row['City'] . " - " . $row['State'] . " - " . $row['PostCode'] . " - " .$row['Description'];
echo "<br />";
mysql_query("INSERT INTO realcas_incard_server.approved_business (customer_id, tradingname)
VALUES ('". $row['customer_id'] ."','".$row['TradingName']."')");
mysql_query("INSERT INTO realcas_incard_server.business_stores (customer_id, storeid, phone, street, suburb, state, postcode, description)
VALUES ('". $row['customer_id'] ."', 1, '".$row['Phone']."', '" .$row['Street'] . "', '" . $row['City'] . "', '" . $row['State'] . "', '" . $row['PostCode'] . "','".$row['Description']."')");
$offerresult = mysql_query("SELECT * FROM customer_realcash_offer WHERE businessid = ".$row['customer_id']);
while($offers = mysql_fetch_array($offerresult))
{
mysql_query("INSERT INTO _incard_server.Real_Cash_Offers (business_id,storeid,offer) VALUES (".$row['customer_id'].",1,'".$offers['offer']."')");
echo $offers['offer']. "<br/>";
}
echo "<br />";
echo "<br />";
}
}
You can either use mysql_select_db() to switch between databases on the same server/username/password/connection, or you can use the database_name.table_name.column_name dotted syntax (made easier with aliases, as in select a.column from database_name.table_name as a).
Those are your two options. (The third option is to just use one database, but I'm assuming that's not an option)
say there is a table test in database first
test
id int
and table junk in database second
junk
id int
then
query would be
insert into junk(id) (select id from first.test);
I'm having a huge issue with this database. It connects correctly and with the information from the form's $_POST queries that are being inserted into the table company_info within the correct fields.
Now, I have no idea what I'm doing wrong here, but I keep getting the die error of
"Error querying database".
The database version is: phpMyAdmin 2.6.4-pl3
MySQL: 5.0
Any ideas? I can provide you the rest of the code if needed.
$dbc = mysql_connect('db390590179.db.1and1.com', 'dbo390590179', '*********')
or die('Error connecting to MySQL server.');
mysql_select_db("db390590179", $dbc);
$query = "INSERT INTO company_info (company_name, company_phone, company_contact, company_address, " .
"company_city, company_state, company_zip, " .
"state_living, vehicles, position, " .
"experience, training, hazmat, " .
"require_hazmat, load_nyc, take_truck_home, " .
"have_rider, have_pet, choose_route, " .
"fuel, cash_advance, days_before_home, " .
"log_system, slip_seat, pre_pass, " .
"ez_pass, health_insurance, retirement_plan, " .
"payment_plan, calculate_pay, freight, " .
"loads, home_on_time, idle_time, " .
"equipment_condition, canada)" .
"VALUES ('$company_name', $company_phone', '$company_contact', '$company_address', '$company_city', " .
"'$company_state', '$company_zip', " .
"'$state_living', '$vehicles', '$position', " .
"'$experience', '$training', '$hazmat', " .
"'$require_hazmat', '$load_nyc', '$take_truck_home', " .
"'$have_rider', '$have_pet', '$choose_route', " .
"'$fuel', '$cash_advance', '$days_before_home', " .
"'$log_system', '$slip_seat', '$pre_pass', " .
"'$ez_pass', '$health_insurance', '$retirement_plan', " .
"'$payment_plan', '$calculate_pay', '$freight', " .
"'$loads', '$home_on_time', '$idle_time', " .
"'$equipment_condition', '$canada')";
$result = mysql_query($query, $dbc)
or die('Error querying database.');
mysql_close($dbc);
I think it's because it's missing a quote before the variable $company_phone in your INSERT statement.
just combine the different values within a single quote.
E.g., "'$company_state , $company_zip,' " ."'$state_living , $vehicles, $position, '" ."'$experience, $training, $hazmat, '" ....
this will work perfectly and also include the missing quote in the begining of the *$company_phone* has to be included.
You can remove double quote from each line and combine them. I have removed syntax errors. You can assure that result is getting or not. Try this code.
$dbc = mysql_connect('db390590179.db.1and1.com', 'dbo390590179', '*********')
or die('Error connecting to MySQL server.');
mysql_select_db("db390590179", $dbc);
$query = "INSERT INTO company_info (company_name, company_phone, company_contact, company_address,
company_city, company_state, company_zip,
state_living, vehicles, position,
experience, training, hazmat,
require_hazmat, load_nyc, take_truck_home,
have_rider, have_pet, choose_route,
fuel, cash_advance, days_before_home,
log_system, slip_seat, pre_pass,
ez_pass, health_insurance, retirement_plan,
payment_plan, calculate_pay, freight,
loads, home_on_time, idle_time,
equipment_condition, canada)
VALUES ('$company_name', '$company_phone', '$company_contact', '$company_address', '$company_city',
'$company_state', '$company_zip',
'$state_living', '$vehicles', '$position',
'$experience', '$training', '$hazmat',
'$require_hazmat', '$load_nyc', '$take_truck_home',
'$have_rider', '$have_pet', '$choose_route',
'$fuel', '$cash_advance', '$days_before_home',
'$log_system', '$slip_seat', '$pre_pass',
'$ez_pass', '$health_insurance', '$retirement_plan',
'$payment_plan', '$calculate_pay', '$freight',
'$loads', '$home_on_time', '$idle_time',
'$equipment_condition', '$canada')";
$result = mysql_query($query, $dbc)
or die('Error querying database.');
mysql_close($dbc);
for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
{
String str = "";
if (CheckBoxList1.Items[i].Selected)
{
str = CheckBoxList1.Items[i].Text;
con.Open();
string sql =
"Insert into dbtable(Category,BookTitle,Feature,SubCategory)values('" +
DDLCategory.SelectedItem.Text + "','" + TxtBooktitle.Text + "','" +
CheckBoxList1.Items[i].Text + "','" + DDLSubcategory.SelectedItem.Text +
"')";
SqlCommand cmd = new SqlCommand(sql, con);
}
}
Just use DEBUGGER and see how things are working and you should be able to resolve such issues easily.