insert into database unknown error - mysql

I am trying to do a simple insert into database but cannot find where the problem is. If anyone could help would be great please. My code:
if(isset($_POST['s1']))
{
$q1 = "INSERT INTO tienda (title,desc) VALUES ('$title', '$desc')";
mysql_query($q1) or die(mysql_error());
echo "<div class=alert fade in><b>Group added!</b></div>";
}
The field side of things:
<tr>
<b>Titulo</b>
<input type=text name=title value="<?=$aset['title']?>" size=50> <br>
</tr>
<tr>
<b>Descripcion</b>
<input type=text name=desc value="<?=$aset['desc']?>" size=50> <br>
</tr>
</div>
</div>
<tr>
<td> </td>
<td>
<input type=submit name=s1 value=Upload class="btn btn-primary">
The error:
You have an error in your SQL syntax; check the manual that corresponds
to your MariaDB server version for the right syntax to use near 'desc)
VALUES (' Title ', '1')' at line 1

desc is a reserved keyword in MySQL (short for description, used in the order by statement). Try enclosing that in backticks like
$q1 = "INSERT INTO tienda (title,`desc`) VALUES ('$title', '$desc')";

Related

How to set Error Messages for a column Element if multiple rows

I want to set an error message for an input tag if the Validator funtion throws an error or fails to validate.
I am able to validate the input but when error is displayed it is displayed to the complete columns elements where as I want for that particular col element.
I was thinking of using index but issue is how will I disable the span tag initially?
<td style="background-color: white;padding:0px;" class="active">
<input class="datepicker_recurring_start" ng-model="item.stdate" placeholder="MM/DD/YYYY" type="text" style="width:73px;height:25px;padding-left:5px;" ng-change="checkStartDate(item.stdate,$index)"/>
<span style="color: red;display:inline;margin-left:10px;" ng-show="startDateCheck" id="adFeeStartDate"> Invalid Start Date </span>
</td>
I am iterating array via ng-repeat so if I have multiple rows, I want error to be shown for first row and currently I am getting error for all rows column element where as only first row column element has been validated.
Here is the Code :
<tr class="newRow" ng-repeat="item in adjustedwrapfee.rows" >
<td style="background-color: white;padding:0px;" class="active">
<input class="datepicker_recurring_start" ng-model="item.stdate" placeholder="MM/DD/YYYY" type="text" style="width:73px;height:25px;padding-left:5px;" ng-change="checkStartDate(item.stdate,$index)"/>
<span style="color: red;display:inline;margin-left:10px;" ng-show="startDateCheck" id="adFeeStartDate"> Invalid Start Date </span>
</td>
</tr>

search 5 product in mysql and echo 4 different rows

im Ivica and im new here.
i need form with 5 places for product codes. Every product have 4 rows (name, perex, amount, library_id) in database and i want place solution every products to different places.
Can help?
Sorry for my english.
DB CODE:
<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'db');
if ($mysqli->connect_error) {
die('Nepodařilo se připojit k MySQL serveru (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$sql = $mysqli->query("SELECT library_id, amount, name, perex, extId
FROM product_item, main_library, product_item_price, product_text
WHERE product_item.product_id = main_library.main_id
AND product_item.id = product_item_price.item_id
AND product_item.product_id = product_text.product_id
AND product_item_price.level_id = 1 AND product_item.extId = '$test'");
echo 'Z databaze jsme ziskali ' . $sql->num_rows . ' radku.';
echo "</br>";
while ($produkt = $sql->fetch_assoc())
{
printf($produkt['name']);
echo "</br>";
printf($produkt['perex']);
echo "</br>";
printf($produkt['amount']);
echo "</br>";
}
$sql->free_result();
$mysqli->close();
?>
Example: I use form and try find 3 different products and these "name", "perex" and "amount" by ID and need to put those results in some tables with any places on page.
FORM:
<form action="search.php" method="REQUEST">
<b>Find product:</b>
<input type="text" name="productID" size="50">
<input type="text" name="productID2" size="50">
<input type="text" name="productID3" size="50">
<br>
<input type="submit" value="Search"> </form>
TABLE:
<table>
<tr>
<td>productID name</td>
<td>productID name</td>
<td>productID name</td>
<tr>
<tr>
<td>productID2 perex</td>
<td>productID2 perex</td>
<td>productID2 perex</td>
<tr>
<tr>
<td>productID3 amount</td>
<td>productID3 amount</td>
<td>productID3 amount</td>
<tr>
</table>
MYSQL look this:
$sql = $mysqli->query("SELECT library_id, amount, name, perex, extId
FROM product_item, main_library, product_item_price, product_text
WHERE product_item.product_id = main_library.main_id
AND product_item.id = product_item_price.item_id
AND product_item.product_id = product_text.product_id
AND product_item_price.level_id = 1
AND (product_item.extId = '$_REQUEST[productID]'
OR product_item.extId = '$_REQUEST[productID2]'
OR product_item.extId = '$_REQUEST[productID3]')");
Can help?
Ivica

Drop-down list using mysql when query not found

I built a simple input drop-down list, using <select> which populates from a mysql database.
It works fine, but if the result from the query is not found then the drop-down list just shrinks and doesn't say anything.
I want it to say something like: "Name not found". I've searched everywhere but I can't seem to find the way.
This is my code:
<?php
if ( $myquery = $mysqli->prepare("SELECT name, idname FROM db WHERE
name LIKE '%".$name."%'") ) {
$myquery->execute();
$myquery->store_result();
$myquery->bind_result( $nompac, $idpac ) ;
}
<form name="form1" method="post" action="example.php">
<table >
<tr>
<td>Name: </td>
<td>
<select name="chosen_name">
<?php
while ( $myquery->fetch() ) {
echo "<strong><option value=".$idpac.">".$nompac."</option></strong>";
}
?>
</select>
</td>
<td><input type="submit" name="Submit" value="Go" class="button"/></td>
</tr>
</table>
</form>
I would like to add an IF statement, saying something like "if $myquery didn't find any results, then $nompac ="name not found". So I wrote this right after the WHILE statement:
if ( $nompac = "" ) {
$nompac = "Name not found";
$idpac = "0";
}
But it just ignores the code as if I didn't write anything :(
Ok I added the code as suggested by Mister Melancholy. Now looks like this:
<form name="form1" method="post" action="example.php">
<table >
<tr>
<td>Name: </td>
<td>
<select name="chosen_name">
<?php
if ( empty( $myquery ) ) {
echo "<strong><option value=''>Name not found</option></strong>";
} else {
while ( $myquery->fetch() ) {
echo "<strong><option value=".$idpac.">".$nompac."</option></strong>";
}
}
?>
</select>
</td>
<td><input type="submit" name="Submit" value="Go" class="button"/></td>
</tr>
</table>
</form>
But still doesn't work if the query doesn't find the name. What am I doing wrong? :-s
I added !empty instead of empty, and I was very happy it seemed to work but it turned out to be that even though the query founded the right name, it echoed "Name not found" every time, so back to square one :(
You need a way to tell if $myquery is empty before you begin your while loop. Something like this should do the trick:
if ( empty( $myquery ) ) {
echo "<strong><option value=''>Name not found</option></strong>";
} else {
while ( $myquery->fetch() ) {
echo "<strong><option value='".$idpac."'>".$nompac."</option></strong>";
}
}
Since I had no further answers in here, I had to ask on another forum and they came up with the solution!
Just to let you know, I used:
if ( $myquery->num_rows==0 ) {
and this works like a charm!

Block user ip registration using mysql, php and html

I would like to know how is it possible to block a list of given ips from a mysql database table of ips.
For example i have a registration form and if the user has a different ip than the one from the mysql dbs he should see the form else he should see a message on that page "You are not allowed to use VPN/Proxy ips on this website".
First how do i create the mysql table and column, what properties do i need to add, so i can import the ips from a csv file.
MYSQL
CREATE TABLE IF NOT EXISTS 'blocked_ips'.......
and don't know how exactly to continue. Tried adding a column in phpmyadmin with VARCHAR(15) and after tried importing the csv file of ips, but it doesn't work, it only imports 2 rows and has only 00 containing in the 2 rows
<?php
//check for user ip
$ip = $_SERVER['REMOTE_ADDR'];
compare the ip got from the user with the mysql table column with ips
if the $ip matches with one from the table echo a message on the same page, (no pop-up).
else {
will echo the form below
?>
<DOCTYPE html!>
<head>
<title> Registration</title>
meta
meta
</head>
<body>
<table class="colortext" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="30%"> </td>
<td height="20" width="70%">{advertiser_msg:8921}</td>
</tr>
<tr>
<td>{advertiser_msg:1092} <span class="mandatory">*</span></td>
<td>{USERFIELD}<span id="fg" style="display: none;white-space: nowrap;"></td>
</tr>
</form>
</body>
</html>
I am a noob in this please help.
mysql_select_db("your database name") or die(mysql_error());
# Full Text Attempt
$query = mysql_query("SELECT `IP` FROM `your table` WHERE `IP` = '$ip'");
or
$query = mysql_query("SELECT `IP` FROM `database` WHERE `IP` LIKE '%$ip%'");
//chk for banned ip ipaddress
if (mysql_num_rows($query) > 0)
{
echo "<p> You are not allowed to register with proxy VPN </p>";
}
?>

You have an error in your SQL syntax

Hi im running into this error and i just cant seem to see the problem so any ideas, a fresh set of eyes might help.
Full 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 'desc='ittititi', price='22', img='img.png'' at line 1
<?php
// Include MySQL class
require_once('../inc/mysql.php');
// Include database connection
require_once('../inc/global.inc.php');
// Include functions
require_once('../inc/functions.inc.php');
// Start the session
session_start();
?>
<?php
// try to create a new record from the submission
$genre = mysql_real_escape_string($_REQUEST['genre']);
$title = mysql_real_escape_string($_REQUEST['title']);
$desc = mysql_real_escape_string($_REQUEST['desc']);
$price = mysql_real_escape_string($_REQUEST['price']);
$img= mysql_real_escape_string($_REQUEST['img']);
if (!empty($genre) && !empty($title) && !empty($desc) && !empty($price) && !empty($img)) {
// here we define the SQL command
$query = "SELECT * FROM books WHERE title='$title'";
// submit the query to the database
$res=mysql_query($query);
// make sure it worked!
if (!$res) {
mysql_error();
exit;
}
// find out how many records we got
$num = mysql_numrows($res);
if ($num>0) {
echo "<h3>That book title is already taken</h3>\n";
exit;
}
// Create the record
$query = "INSERT INTO books SET genre='$genre', title='$title', desc='$desc', price='$price', img='$img'";
$res = mysql_query($query)or die(mysql_error());
if (! $res) {
echo mysql_error();
exit;
} else {
echo "<h3>Book Created</h3>\n";
echo $_SESSION['title']=$title;
}
}
?>
<form name="newbook" method="post">
<table border=0>
<tr>
<td>Genre:</td>
<td><input type=text name='genre'></td>
</tr>
<tr>
<td>Title:</td>
<td><input type=text name='title'></td>
</tr>
<tr>
<td>Description:</td>
<td><input type=text name='desc'></td>
</tr>
<tr>
<td>Price:</td>
<td><input type=number name='price'></td>
</tr>
<tr>
<td>Image:</td>
<td><input type=text name='img'></td>
</tr>
<tr>
<td colspan=2>
<input type=submit value="Create my account">
</td>
</tr>
</table>
</form>
You need to escape reserved words in MySQL like desc with backticks
INSERT INTO books
SET genre = '$genre', title = '$title', `desc` = '$desc'
^----^-----------------here
desc is reserved keyword for mysql
use it like that
`desc`
this must ne your query
$query = "INSERT INTO books SET genre='$genre', title='$title', `desc`='$desc', price='$price', img='$img'";
Don't use desc as a column name; it is a keyword. If you use it as a column name, you have to quote it.