{php} Column count doesn't match value count at row 1 - php

I make a add forum
if ($_SERVER['REQUEST_METHOD'] == "POST" AND isset ($_REQUEST['addfinal'])) {
$name_1 = clear_form_text ($_REQUEST['name_1']);
$name_2 = clear_form_text ($_REQUEST['name_2']);
$car_name = intval ($_REQUEST['id_car_name']);
if (empty ($name_1)) $stop .= "Error text!<br>";
if (empty ($name_2)) $stop .= "Error text!<br>";
if ($stop == false) {
$db->query ("INSERT INTO car_class (name_1, name_2, id_car_name) VALUES ('$name_1', '$name_2' '$car_name')") or die ($db->error);
$infomessage = "<div class=\"attention-box attention-success\"><p class=\"text-muted\">Add success. Back</p></div>";
} else {
$infomessage = "<div class=\"attention-box attention-danger\"><p class=\"text-muted\">" . $stop . " Врати се назад</p></div>";
}
} else {
$result = $db->query("SELECT id, name FROM car_name");
while ($row = $result ->fetch_array())
$arrayrow[] = $row;
foreach ($arrayrow as $row) {
$select .= "<option value='".$row['id']."'>".$row['name']."</option>";
}
$template->set_block ('car_class-add', '', 'car_class');
$template->set ('car_name', $select, 'car_class');
}
The Select need to read values from car_name (id, name) and writes it id in car_class
But when i click on submit give me
Column count doesn't match value count at row 1
Where is the problem with this code?
Thank you
EDIT
Problem from above is solved.
NOW script work but do not write value in id_car_name Only write 0
car_class.id_car_na‌​me = car_name.id Why still this problem?

You must add comma after '$name_2' :
$db->query ("INSERT INTO car_class
(name_1, name_2, id_car_name)
VALUES ('$name_1', '$name_2', '$car_name')")
or die ($db->error);

Related

PDO - Test Of Empty Results

I have a query that should look for an entry. If it's not in the database then enter in the data. Otherwise it returns back the data and they can update any fields. If there is an entry it will be only one. This works great if the entry is in the table. But I've tried checking for empty rows, doing row_count, etc and doesn't seem to work. Right now I just have this in the code(sanitized to remove company table information):
$query1 = " SELECT Number, Notes, Qty1, Qty2 FROM test.notes ";
$query1 .= " WHERE Number = '$searchnumber' ";
$result1 = $conn1->query($query1);
$conn1 = null;
if($result1==null)
{
echo "Result is null</p>\n";
return 0;
}
else
{
echo "Result is not null</p>\n";
return $result1;
}
If I take out the if check what I seem to get back is if it's found it returns the values correctly. If it's not found the result seems to be the query string itself. The check doesn't work. Probably because it returns back the query string if it's not found.
I know it's something simple but just haven't found it.
// if available in database
$query="SELECT Number, Notes, Qty1, Qty2 FROM test.notes WHERE Number='".$searchnumber."'";
$qnt = $conn1->query($query);
$coun = count($qnt->fetchAll());
if($coun > 0){
// available
echo "Result is available</p>\n";
}else{
//not available
echo "Result is not available</p>\n";
}
i Think you need something like this.
if this is not working fine, try another aproach
$queryi = $conn1->prepare("SELECT Number, Notes, Qty1, Qty2 FROM test.notes WHERE Number='".$searchnumber."' ");
$queryi->execute();
$qn= $queryi->fetchAll(PDO::FETCH_ASSOC);
foreach ($qn as $row => $data) {
$in_use = $data['Number'];
//echo $in_use ;
}
// evaluate
if($in_use == NULL){
//not avilable
}else{
// available
}
I suggest doing something like this:
Establish your query
$query1 = " SELECT Number, Notes, Qty1, Qty2 FROM test.notes ";
$query1 .= " WHERE Number = '$searchnumber' ";
See if there's a result for the query, and no error
if ($res = $conn1->query($sql)) {
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
/* Issue the real SELECT statement and work with the results */
$sql = "SELECT name FROM fruit WHERE calories > 100";
foreach ($conn->query($sql) as $row) {
print "Name: " . $row['NAME'] . "\n";
}
}
/* No rows matched -- do something else */
else {
print "No rows matched the query.";
}
}
After some trial and error I got this to work:
$result1 = $conn1->query($query1);
$count = $result1->fetchColumn();
if($count == "")
{
// echo "Result is null</p>\n";
return "0";
}
else
{
// echo "Result is not null</p>\n";
$result1 = $conn1->query($query1);
return $result1;
}
I had to change the setup to include:
$conn1->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE);
Probably not a clean way but it works for now. Thanks for all the help.

Not able to get proper value and print respective msg

Below is my code , I am trying to append values of $vid ,and than next time when my file loads i am trying to find currently passed value of $vid in array $ids, but every time i am getting success msg ,where i am going wrong, am i not able to append values properly ?
process.php
session_start();
if(isset($_POST)) {
$reason_data = trim($_POST['abuse_data']);
if($reason_data == "Video is offensive")
{
$reason = 'a:1:{i:0;s:18:"' . $reason_data .'";}';
}
else
{
$reason = 'a:1:{i:0;s:19:"' . $reason_data .'";}';
}
$motive = trim($_POST['remark_data']);
$uid = trim($_POST['abuse_uid']);
$vid = trim($_POST['abuse_vid']);
if(trim($_SESSION['abuse_vdo_id']) == '')
{
$_SESSION['abuse_vdo_id'] = $vid;
$str = $_SESSION['abuse_vdo_id'];
}
else {
$str = $str."," .$vid;
$_SESSION['abuse_vdo_id'] = $str;
}
print_r($_SESSION['abuse_vdo_id']);
echo "break";
print_r($str);
$ids = explode(',',$_SESSION['abuse_vdo_id']);
echo "break 2";
print_r($ids);
if(in_array($vid, $ids))
{
echo "already abused";
}
else
{
echo "success";
$query = "INSERT INTO ".DB_PREFIX."reports ( reason, motive, uid, vid ) VALUES ( '{$reason}','{$motive}', '{$uid}', '{$vid}' )";
$result = mysql_query($query);
}
}

php mysql insert and update

Hy there, I guess I've tried everything I could. Somehow my form doesn't work.
I get an ID through the URL - Which I try to use to update the MySQL table
I use the same Form also to input a new Record and there is no problem. But to update it doesn't work at all.
if (true !=$fehler)
{
if ($clientid == 'new')
{
$qy = 'INSERT INTO tbl_clientdb (
clientid,
c_update,
c_Uupdate,
c_Gender,
c_IDNumber,
c_Name,
c_Firstname,
c_Middlename,
c_idCity,
c_idCountry,
c_idLanguage,
c_Phone,
c_Cellphone,
c_Email,
c_Note,
c_idCompany
)
VALUES (
NULL,
NOW(),
"'.$c_Uupdate.'",
"'.$c_Gender.'",
"'.$c_IDNumber.'",
"'.$c_Name.'",
"'.$c_Firstname.'",
"'.$c_Middlename.'",
"'.$c_idCity.'",
"'.$c_idCountry.'",
"'.$c_idLanguage.'",
"'.$c_Phone.'",
"'.$c_Cellphone.'",
"'.$c_Email.'",
"'.$c_Note.'",
"'.$c_idCompany.'"
)';
} else {
$qy = 'UPDATE
tbl_clientdb
SET
c_update = NOW(),
c_Uupdate = "'.$c_Uupdate.'",
c_Gender = "'.$c_Gender.'",
c_IDNumber = "'.$c_IDNumber.'",
c_Name = "'.$c_Name.'",
c_Firstname = "'.$c_Firstname.'",
c_Middlename = "'.$c_Middlename.'",
c_idCity = "'.$c_idCity.'",
c_idCountry = "'.$c_idCountry.'",
c_idLanguage = "'.$c_idLanguage.'",
c_Phone = "'.$c_Phone.'",
c_Cellphone = "'.$c_Cellphone.'",
c_Email = "'.$c_Email.'",
c_Note = "'.$c_Note.'",
c_idCompany = "'.$c_idCompany.'"
WHERE
clientid = '.$clientid.'
LIMIT 1';
}
if ($res = mysql_query($qy))
{
echo 'Your data has been saved successfully';
}
else
{
echo mysql_error();
$meld = 'Please try again';
}
}`
anybody an idee what could be the trouble?
thx a lot for any inputs.
$qy = 'SELECT
clientid,
c_update,
c_Uupdate,
c_Gender,
c_IDNumber,
c_Name,
c_Firstname,
c_Middlename,
c_idCity,
c_idCountry,
c_idLanguage,
c_Phone,
c_Cellphone,
c_Email,
c_Note,
c_idCompany
FROM tablename WHERE id='$id' LIMIT 1";
$query = mysqli_query($yourConnection, $qy) or die (mysqli_error());
While ($row = mysqli_fetch_array($query)) {
/* Create a variable to hold all the data */
$c_update = $row['c_update'];
/* Do same for the rest */
}
Mysqli_free_result($query);
?>
Hope this helps

PHP Validating Submit

I'm working on a project where a user can click on an item. If the user clicked at it before , then when he tries to click at it again it shouldn't work or INSERT value on the DB. When I click the first item(I'm displaying the items straight from database by id) it inserts into DB and then when I click at it again it works(gives me the error code) doesn't insert into DB. All other items when I click at them , even if I click for the second, third, fourth time all of it inserts into DB. Please help guys. Thanks
<?php
session_start();
$date = date("Y-m-d H:i:s");
include("php/connect.php");
$query = "SELECT * FROM test ORDER BY `id` ASC LIMIT 3";
$result = mysql_query($query);
if (isset($_SESSION['username'])) {
$username = $_SESSION['username'];
$submit = mysql_real_escape_string($_POST["submit"]);
$tests = $_POST["test"];
// If the user submitted the form.
// Do the updating on the database.
if (!empty($submit)) {
if (count($tests) > 0) {
foreach ($tests as $test_id => $test_value) {
$match = "SELECT user_id, match_id FROM match_select";
$row1 = mysql_query($match)or die(mysql_error());
while ($row2 = mysql_fetch_assoc($row1)) {
$user_match = $row2["user_id"];
$match = $row2['match_id'];
}
if ($match == $test_id) {
echo "You have already bet.";
} else {
switch ($test_value) {
case 1:
mysql_query("UPDATE test SET win = win + 1 WHERE id = '$test_id'");
mysql_query("INSERT INTO match_select (user_id, match_id) VALUES ('1','$test_id')");
break;
case 'X':
mysql_query("UPDATE test SET draw = draw + 1 WHERE id = '$test_id'");
mysql_query("INSERT INTO match_select (user_id, match_id) VALUES ('1','$test_id')");
break;
case 2:
mysql_query("UPDATE test SET lose = lose + 1 WHERE id = '$test_id'");
mysql_query("INSERT INTO match_select (user_id, match_id) VALUES ('1','$test_id')");
break;
default:
}
}
}
}
}
echo "<h2>Seria A</h2><hr/>
<br/>Welcome,".$username."! <a href='php/logout.php'><b>LogOut</b></a><br/>";
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$home = $row['home'];
$away = $row['away'];
$win = $row['win'];
$draw = $row['draw'];
$lose = $row['lose'];
echo "<br/>",$id,") " ,$home, " - ", $away;
echo "
<form action='seria.php' method='post'>
<select name='test[$id]'>
<option value=\"\">Parashiko</option>
<option value='1'>1</option>
<option value='X'>X</option>
<option value='2'>2</option>
</select>
<input type='submit' name='submit' value='Submit'/>
<br/>
</form>
<br/>";
echo "Totali ", $sum = $win+$lose+$draw, "<br/><hr/>";
}
} else {
$error = "<div id='hello'>Duhet te besh Log In qe te vendosesh parashikime ndeshjesh<br/><a href='php/login.php'>Kycu Ketu</a></div>";
}
?>
Your problem is here :
$match = "SELECT user_id, match_id FROM match_select";
$row1 = mysql_query($match)or die(mysql_error());
while ($row2 = mysql_fetch_assoc($row1)) {
$user_match = $row2["user_id"];
$match = $row2['match_id'];
}
You are not checking it correctly. You have to check if the entry in match_select exists for the user_id and the match_id concerned. Otherwise, $match would always be equal to the match_id field of the last inserted row in your database :
$match = "SELECT *
FROM `match_select`
WHERE `user_id` = '<your_id>'
AND `match_id` = '$test_id'";
$matchResult = mysql_query($match)or die(mysql_error());
if(mysql_num_rows($matchResult)) {
echo "You have already bet.";
}
By the way, consider using PDO or mysqli for manipulating database. mysql_ functions are deprecated :
http://www.php.net/manual/fr/function.mysql-query.php
validate insertion of record by looking up on the table if the data already exists.
Simplest way for example is to
$query = "SELECT * FROM match_select WHERE user_id = '$user_id'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0)
{
// do not insert
}
else
{
// do something here..
}
In your form you have <select name='test[$id]'> (one for each item), then when you submit the form you are getting $tests = $_POST["test"]; You don't need to specify the index in the form and can simply do <select name='test[]'>, you can eventually add a hidden field with the id with <input type="hidden" value="$id"/>. The second part is the verification wich is not good at the moment; you can simply check if the itemalready exist in the database with a query

While parsing xml/json, skip page if error message received

I have following issue. I am parsing an xml feed with multiple pages and there is no way to understand how many they are (I have a variavle set in the xml url, so for each variable there are random number of pages). All I know is that the number can not exceed 50. Now I have a script that autoincrement the variable for the page number starting at 1 and up to 50.
Let's say, that the link has total of 26 pages. With my script I will continue to send requests until the scripts gets to page 50. Than it changes the first variable and starts again from 1 to 50. For the first link, from page 27 forward the xml will return followwing:
<response>
<status>error</status>
<code>400</code>
<message>Incorrect Request Headers</message>
</response>
How can I make so when the scipt receive this message, to stop the autoincrement and continue by changing the first variable and start again at 1? The code now is:
$query = "SELECT * FROM table_name ORDER BY id ASC";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
if($row['row_name'] == '') {
$variable1 = 0;
}
else {
$variable1 = $row['row_name'];
}
$page = 0;
do {
$page++;
$result = apiCall('option1', 'option2', array('option3' => $variable1, 'page' => $page));
usleep(1000);
$res = json_decode($result);
foreach ($res->node1->node2 as $item) {
//define variable for insertion in MySQL
$sql1 to insert the variables
if (!mysql_query($sql1,$con1))
{
die('Error: ' . mysql_error());
}
}
}
while ($page<=50);
}
In the above code, only the $variable1 and $page are variables. All options (1, 2 and 3) are predefined and stay the same. I.e. I need when the script gets the error message to start again from 1 with next value of $variable1.
OK. Here is the code I played around:
$query = "SELECT * FROM table_name ORDER BY id ASC";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
if($row['row_name'] == '') {
$variable1 = 0;
}
else {
$variable1 = $row['row_name'];
}
$page = 1;
do {
$result = apiCall('option1', 'option2', array('option3' => $variable1, 'page' => $page));
usleep(1000);
$res = json_decode($result);
if ($res->status == 'error') {
break 2;
}
foreach ($res->node1->node2 as $item) {
//define variable for insertion in MySQL
$sql1 to insert the variables
if (!mysql_query($sql1,$con1))
{
die('Error: ' . mysql_error());
}
}
$page++;
}
while (0);
}
The problem is that it stops the script execution at some point. Can not understand why.

Categories