Unexpected $end in php script? - php

I have this bit of code that looks fine to me, but I keep getting the following error:
"Parse error: syntax error, unexpected $end in
/home/txclanco/public_html/hotmuze/addSong.php on line 21"
This is a script that takes in the input from a form, checks for duplicates and if there is a duplicate, it will not make a new row but just add 1 to the 'rating' row where the songname is the song typed in. If there isn't a duplicate, the script will add the data as a new row. The data types are:
id = A_I/int
songname = varchar
artist = varchar
rating = int
The script is below with the mysql data blanked out:
<?
mysql_connect("localhost", "***", "***") or die(mysql_error());
mysql_select_db("***") or die(mysql_error());
$songname = $_POST['songname'];
$artist = $_POST['by'];
$ratenum = 1;
$chkquery = "SELECT * FROM hotmuze WHERE songname='$songname'";
$plusOneQuery = "SELECT * FROM hotmuze WHERE songname='$songname'";
$updateQuery = "UPDATE hotmuze SET rating='$rating2' WHERE songname='$songname'";
$checkdata = mysql_query($chkquery);
$checkrows = mysql_num_rows($checkdata);
if($checkrows==0)
{
$insquery = "INSERT INTO hotmuze (id, songname, artist, rating) VALUES('', '$songname', '$artist', '$ratenum'";
$insdata = mysql_query($insquery);
}
if($checkrows!=0)
{
$plusData = mysql_query($plusOneQuery);
}
if(mysql_num_rows($plusData)!=0)
{
$result = mysql_fetch_assoc($plusData);
$rating = $result['ratng'];
$rating2 = $rating + 1;
mysql_query($updateQuery);
echo "Data Inserted";
}
?>
Line 21 being:
if($checkrows!=0)
{
// The brace is on line 21
$plusData = mysql_query($plusOneQuery);
}
Any ideas what could be wrong with the script? I know the Unexpected $end error usually means that there's a brace out of place, but this time, it's fine?

change this
$insquery = "INSERT INTO hotmuze (id, songname, artist, rating) VALUES('', '$songname', '$artist', '$ratenum'";
to
$insquery = "INSERT INTO hotmuze (id, songname, artist, rating) VALUES('', '$songname', '$artist', '$ratenum')";
you have missed the ) at end

VALUES open and close '()' is need
$insquery = "INSERT INTO hotmuze (id, songname, artist, rating)
VALUES('', '$songname', '$artist', '$ratenum')";

Count the number of opening brackets you have against the number of closing brackets.
$insquery = "INSERT INTO hotmuze (id, songname, artist, rating) VALUES('', '$songname', '$artist', '$ratenum')";

Related

SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' on line 2

Can you help me guys to fix my error, I stack with this error message the problem is only when inserting data into database.
I don't know if my code is an old coding style.
I badly need your help guys sorry for my English.
<?php
if ($sID > 0) {
$query = "UPDATE `shows`
SET video = '$video',
title = '$title',
description = '$description',
content = '$content',
episodes = '$episodes',
duration = '$duration',
pilot_rating = '$pilot_rating',
pilot_share = '$pilot_share',
sold_in = '$sold_in',
production = '$production',
marketing = '$marketing',
rating = '$rating',
awards = '$awards',
alias = '$alias',
publish = '$publish',
posted = '$posted',
`show` = '1',
catid2 = '$catID2',
catid = '$catID',
modified_date = '$modified_date',
modified_by = '".$_SESSION['cpuid']."',
now_available = '$now_available',
all_titles = '$all_titles',
hot_clips = '$hot_clips',
featured_program = '$featured_program'
WHERE sid = '$sID'";
} else {
$query = "INSERT INTO `shows` ( video, title, description, content, episodes, duration, pilot_rating, pilot_share, sold_in, production, rating, awards, alias, publish, posted, show, catmain, catid2, catid, created_by, modified_date, modified_by, now_available, all_titles, hot_clips, featured_program ) VALUES ('$video', '$title', '$description', '$content', '$episodes', '$duration', '$pilot_rating', '$pilot_share', '$sold_in', '$production', '$rating', '$awards', '$alias', '$publish', '$posted', '1', '1', '$catID2', '$catID', '".$_SESSION['cpuid']."', '$modified_date', '".$_SESSION['cpuid']."', '$now_available', '$all_titles', '$hot_clips', '$featured_program' )";
}
mysql_query($query) or die('Error, query failed'.mysql_error());
?>

Error Column count doesn't match value count at row 1?

where is error.....
Column count doesn't match value count at row 1
mysqli_select_db($conn, $data);
$save = "INSERT INTO SONG_AS(ALBUM, CATEGORY, SUB_CATEGORY,
SONG_NAME, ARTIST, ART_LINK,
YEAR, GENRE, SONG_LINK, POST_ON,
POST_BY, TOTAL_DOWNLOAD)
VALUES('$albm', '$cat', '$scat', "
. "'$sn', '$art', '$img', "
. "'$y', '$g', "
. "'$sl', '$time', '', '0')";
$success = mysqli_query($conn, $save) or die(mysqli_error($conn));
$page = "index.php";
$this->pageRedirect($page);
where is error.....
Column count doesn't match value count at row 1
remove '' from your query to be:
$save = "INSERT INTO SONG_AS(ALBUM, CATEGORY, SUB_CATEGORY,
SONG_NAME, ARTIST, ART_LINK,
YEAR, GENRE, SONG_LINK, POST_ON,
POST_BY, TOTAL_DOWNLOAD)
VALUES('$albm', '$cat', '$scat','$sn', '$art', '$img', '$y', '$g', '$sl', '$time', '', '0')";
Try this:
$save = "INSERT INTO SONG_AS(`ALBUM`, `CATEGORY`, `SUB_CATEGORY`,
`SONG_NAME`, `ARTIST`, `ART_LINK`,
`YEAR`, `GENRE`, `SONG_LINK`, `POST_ON`,
`POST_BY`, `TOTAL_DOWNLOAD`)
VALUES('".$albm."', '".$cat."', '".$scat."','".$sn."', '".$art."', '".$img."', '".$y."', '".$g."', '".$sl."', '".$time."', '', '0')";
$query = "INSERT INTO employee VALUES ($empno','$lname','$fname','$init','$gender','$bdate','$dept','$position','$pay','$dayswork','$otrate','$othrs','$allow','$advancesance,'')";
$msg = "New record saved!";
}
else {
$query = "UPDATE employee SET empno=$empno','lname='$lname',fname='$fname',init= '$init',gender='$gender',bdate='$bdate',dept='$dept',position='$position',pay=$pay,dayswork=$dayswork,otrate=$otrate,othrs=$othrs,allow=$allow,advances=$advances,insurance=$insurance WHERE empno = $empno";
$msg = "Record updated!";
}

Use returned ID from SCOPE_IDENTITY in new Query

Right now, this is what I have:
$query = "INSERT INTO COMMENTS VALUES ('$user', '$comment', '$star')";
mssql_query($query, $connection);
$commentIDQuery = "SELECT SCOPE_IDENTITY() AS ins_id";
$CI = mssql_query ($commentIDQuery, $connection);
$commentID = mssql_fetch_row($CI);
$idQuery = "SELECT recipeid FROM t_recipe WHERE recipename = '$recipeName'";
$RID = mssql_query($idQuery, $connection);
$recipeID = mssql_fetch_row($RID);
$rcQuery = "INSERT INTO COMMENT_RECIPE VALUES ('$commentID[0]', '$recipeID[0]')";
mssql_query($rcQuery, $connection);
So how would I get that ins_id?
It adds it to the first table, which is comments, but not the relation table.
Using sql server 2008
What about this......
$query = "DECLARE #NewID INT
INSERT INTO COMMENTS VALUES ('$user', '$comment', '$star');
SELECT #NewID = SCOPE_IDENTITY();
INSERT INTO COMMENTS_RECIPE VALUES (#NewID, '$recipeid')";
$stmt = sqlsrv_query($conn,$query);

What is proper way to skip a MySQL INSERT

I have a foreach statement looping through JSON data and inserting the contents into MySQL. I want to skip the insert if a specific username is shown for $author string. Is the below method ok or is it better to handle at the database level?
foreach ($response['data'] as $data) {
$id = $data['id'];
$created_time = $data['created_time'];
$thumbnail = $data['images']['low_resolution']['url'];
$author = $data['user']['username'];
$caption = mysql_real_escape_string($data['caption']['text']);
$link = $data['link'];
$likes = $data['likes']['count'];
if ($author == 'USERNAME') {
mysql_close($con);
} else {
$query = "INSERT IGNORE INTO pbr (id, created_time, thumbnail, author, caption, link, likes, hash) VALUES ('$id', '$created_time', '$thumbnail', '$author', '$caption', '$link', '$likes', '$hash')";
$result = mysql_query($query) or die(mysql_error());
mysql_close($con);
}
}
Why closing SQL connection at each loop iteration?
Why not simply do:
if ($author == 'USERNAME')
continue; // next iteration
$query = "INSERT IGNORE INTO pbr (id, created_time, thumbnail, author, caption, link, likes, hash)
VALUES ('$id', '$created_time', '$thumbnail', '$author', '$caption', '$link', '$likes', '$hash')";
$result = mysql_query($query) or die(mysql_error());
BTW you should bind parameters to your queries, or at least use mysql_real_escape_string() otherwise will have problems with values containing quotes (currently, you only do it for variable $caption, I guess that $link, $thumbnail and $username can contain single quotes as well).

Update FK from PK using PHP-MySQL

i'm new in php and mysql
i have a problem
i have 2 tables
<?php
$insert = mysql_query("INSERT INTO request (date, type_request, subject, customer)
VALUES (NOW(), '".$type."', '".$subject."', '".$username."')");
$fk = mysql_query("insert into feedback (id_request) select id_request from request where id_request = last_insert_id ");
?>
i've been doing that but still cannot fill the id_request in table feedback
the structure of table is like this
Table Request
id_request auto_increment not_null,-->PK
date,
type_request,
subject,
customer
Table Feedback
id_feedback auto_increment not_null,
id_request,---FK
feedback_user
can anyone give suggest how to update the foreign key
Regards
In your code
$fk = mysql_query("insert into feedback (id_request) select id_request from request where id_request = last_insert_id ");
replace last_insert_id with LAST_INSERT_ID()
since its a MySQL function and not a field.
I know I will get flamed for this, but this is how I would do it:
<?php
$date = date('Y-m-d H:i:s');
$req_query = 'INSERT INTO request (date, type_request, subject, customer) '.
"VALUES ('$date', '$type', '$subject', '$username')";
$req_result = mysql_query($req_query);
$fk_query = 'SELECT MAX(id) id FROM request '.
"WHERE date = '$date' AND type_request = '$type' ".
"AND subject = '$subject' AND customer = '$username'";
$fk_result = mysql_query($fk_query);
$fk_row = mysql_fetch_assoc($fk_result);
$fk = $fk_row['id'];
$fb_result = mysql_query("INSERT INTO feedback (id_request) VALUES($fk)");
?>

Categories