Problems deleting a row with php - php

I'm trying to remove a row from a table with php. I have the following piece of code:
<?php
if($_GET["act"] == 'eliminarart' && $_GET["id"] != '')
{
var_dump($_GET);
if($mysql->query("DELETE FROM landing_articulos WHERE id_articulo = ". intval($_GET["id"])." LIMIT 1")){
redirige('nuevapagina.php');
} else {
$e=$mysqli->error;
echo $e;
}
}
?>
<head>
<script language="javascript">
function eliminar_art(id){
var mensaje = 'Deseas eliminar este articulo?';
eliminar = confirm(mensaje);
if (eliminar)
document.location='nuevapagina.php?act=eliminarart&id='+id;
}
</script>
</head>
I call all this code here:
while($f1 = mysql_fetch_array($c1)){
?>
<div style="float:left; width:32%; padding-left:0px; color:#ef5000; font-weight:bold; margin:5px;"><p><strong><?=$f1["nombre"];?></strong> <img src="imagenes/lapiz.png" width="15" height="15" border="0"/> </a> <img src="imagenes/delete.png" width="15" height="15" border="0"/> <? if($f1["valido"]=='0')echo ' - SIN VALIDAR';?></p></div><?
}
The var_dump returns data, although the id I get is a string instead of an int as it's defined in the database. So what I am doing wrong? All this code is in a page called nuevapagina.php.

no need to convert into integer just simply do it as:
$mysql->query("DELETE FROM landing_articulos WHERE id_articulo = ".$_GET["id"]." LIMIT 1"

Related

Can't retrieve numeric input and assign it to a variable

I'm tring to retrieve the numeric input and store it in the variabe q but I can't seem to access it using the method post this way.
<?php
$sql = "SELECT * FROM produit";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)){
?>
<div class="prod<?php echo $row['ID_produit']?>">
<img src="images/<?php echo $row['image']?>" alt="image">
<h3><?php echo $row['nom_produit']?></h3>
<h3>Prix:<?php echo $row['prix']?>€</h6>
<form action="" method="post">
<label name="qte">QTE:</label>
<input type="number" name="qte"><br><br>
<a href="panier.php?action=ajout&
l=<?php echo $row['nom_produit']?>&
q=<?php $_POST['qte']?>&
p=<?php echo $row['prix']?>"
onclick="window.open(this.href, '', 'toolbar=no, location=no, directories=no, status=yes,
scrollbars=yes, resizable=yes, copyhistory=no, width=600, height=350'); return false;">
GOGOGO </a>
</form>
</div>
<?php
}
?>
</div>
I also tried with
<?php
if (isset($_POST['submit'])) {
$example = $_POST['qte'];
echo $example;
}
?>
There's a couple issues with this code. You were trying to reference qte in each loop through a $_POST variable, when what you really wanted was to just grab the input from the user. In an effort to clean up the code a little and make things more readable, I changed that big <a href... tag to trigger a function that will grab the qte value, form the URL and open the window. Note that I pass into the function the productID and prix values. I put them in quotes just in case they're blank. I also added an ID to your qte input element so we can get the value. Take a look and let me know if you need clarification
<?php
$sql = "SELECT * FROM produit";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)){
?>
<div class="prod<?php echo $row['ID_produit']?>">
<img src="images/<?php echo $row['image']?>" alt="image">
<h3><?php echo $row['nom_produit']?></h3>
<h3>Prix:<?php echo $row['prix']?>€</h6>
<form action="" method="post" onsubmit="return false;">
<label for="qte">QTE:</label>
<input type="number" name="qte" id="qte_<?php echo $row['ID_produit']?>"><br><br>
<a href='javascript:void(0)'
onclick='gogoLink("<?php echo $row['ID_produit']?>", "<?php echo $row['prix']?>")'>
GOGOGO </a>
</form>
</div>
<?php
}
?>
// then outside the loop is the function
<script>
function gogoLink(id, prix) {
let url ="panier.php?action=ajout"
url += "&l=" + id
url += "&q=" + document.getElementById("qte_"+id).value;
url += "&p=" + prix;
window.open(url, '', 'toolbar=no, location=no, directories=no, status=yes,
scrollbars=yes, resizable=yes, copyhistory=no, width=600, height=350');
}
</script>

php and jquery cannot run through all conditions

I'm writing a code for my website. in this I use drag-drop to give point.
This is my jQuery code:
$('#click').click(function() {
$('#id0').ready(function() {
if ($(this).find('img#e1').length) {
a = 3;
} else if ($(this).find('img#e2').length) {
a = -3;
} else {
a = 0
};
$.post('php/noname1.php', {
point: a
})
});
});
and this is my php code
<?php
$con=mysqli_connect("localhost","name","pass","data") ;
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$point=$_POST['point'];
mysqli_query($con,"INSERT INTO test (point, part, comment) values ($point,'first','good')");
?>
When I click on #click, the php runs but in the database it only gets 3, even though I've changed the img from #e1 to #e2. I think the problem is in the jQuery code, but I don't see anything wrong in the code. Anybody know ?
Here's the html:
<body>
<header>
<div id='header'><img id='logo' src='photo/prlogo.jpg' width='420' height='120'></div>
</header>
<div id='body'>
<div id='product'><img id="product" src="photo/pr.jpg" ></div>
<div id='face'>
<img id="e1" class='e' src="photo/hap.png" width="50" height="50">
<img id="e2" class='e' src="photo/sad.png" width="50" height="50">
</div>
<button type="button" id='click' >Send</button>
<button type="button" id='undo'>Change your mind ?</button>
</div>
and this is how I create #id0, it's a div, droppable, I divided the picture into 5 div, #id0 is one of them:
for ( var i = 0; i < d; i++ ) {
divi.push('<div id="id' + i + '" class="tile" />');
}
I will assume that #id0 is your drop zone
HTML
<div id='product'><img id="product" src="photo/pr.jpg" ></div>
<div id='face'>
<img id="e1" class='e' src="photo/hap.png" width="50" height="50">
<img id="e2" class='e' src="photo/sad.png" width="50" height="50">
</div>
<div id="id0"></div>
<button type="button" id='click'>Send</button>
<button type="button" id='undo'>Change your mind ?</button>
jQuery
$('#click').click(function() {
if ($('#id0 img#e1').length) {
a = 3;
} else if ($('#id0 img#e2').length) {
a = -3;
} else {
a = 0
};
console.log(a)
});
Here's a fiddle http://jsfiddle.net/6JQxW/

php database, jquery dynamic content load

I would like to achieve such an effect, except that when you click on the picture (link) jquery script sends a GET to the same file from the fact that with another ID. I mean the dynamic reload the page without refreshing the addition of a nice effect in the attached link.
my code :
<script>
$('a.menu').click(function(){
$('.content').html('');
})
</script>
<div class="content" id="page_effect" style="padding:0px; display:none;">
<div class="separator" style="margin: -17px auto;"></div>
<span class="choose-product"> Wybierz Produkt</span>
<p>
<?php
$kat=$_GET['kat'];
$co_ile_strona=9;
//----------------
$dopisz="";
if (is_numeric($kat)) {
$dopisz=" WHERE kat_id='".$kat."'";
$wyk=mysql_query("SELECT * FROM kategorie WHERE kat='".$kat."'");
while($ww=mysql_fetch_array($wyk)) {
$dopisz.=" OR kat_id='".$ww['id']."'";
}
}
$sile=false;
$wyk=mysql_query("SELECT * FROM produkty ".$dopisz."");
if ($ile=mysql_num_rows($wyk)) {
if (!$sile) {
$nazwa = mysql_fetch_assoc(mysql_query("SELECT * FROM kategorie WHERE id='".$_GET['kat']."'"));
if(strlen($nazwa['nazwa']) > 0)
$nazwa = $nazwa['nazwa'];
?>
<div style="text-align: center; width: 80%;margin: 0 auto;margin-top: 39px;">
<a href="produkt.html"><div class="product-box">
<img src="images/picasso0.png" alt="Product"/>
<span class="product-title"><?=$nazwa?></span>
</div>
</a>
<?
$sile=true;
}
if (!$_GET['strona']) $strona=1; else $strona=$_GET['strona'];
$start=($strona*$co_ile_strona)-$co_ile_strona;
mysql_data_seek($wyk,$start);
$licz=0;
while(($ww=mysql_fetch_object($wyk)) && $licz<$co_ile_strona) { $licz++;
?>
<a href="<?=strtolower(seo($ww->nazwa))?>-<?=$ww->id?>p.html"><div class="product-box">
<img src="produkty/front/<?=$ww->front?>" alt="<?=$ww->nazwa?>"/>
<div class="name2"><span><?=$ww->nazwa?> </span></div>
</div>
</a>
<?
}
} else echo "<span style='color: #ff0000; font-size: 12pt; font-weight: bold;'>Przepraszamy, ale nie znaleziono produktów pasujących do tego zapytania</span>";
?>
</div>
<div class="menu-bottom" style="text-align:center;">
<span style="position: relative;top: 25px;display: inline-flex;margin-bottom: 20px;">Wybierz serię:
<ul>
<?php
$zapas=$_GET['kat'];
$wyk=mysql_query("SELECT * FROM kategorie WHERE kat='0' and wid='1' ORDER BY poz ASC");
while($ww=mysql_fetch_object($wyk)) {
?> <!--<?/*=$ww->nazwa?>-<?=$ww->id*/?>k.html*/-->
<li> <? if($_GET['kat']==$ww->id) echo "<span style='color: #000;'>".$ww->nazwa.""; else echo $ww->nazwa?></li>
<? } ?>
</ul>
</span>
</div>
<!-- end .content --></div>
link : Click here
As mentioned in the comment, you should send an AJAX request to the page that is responsible for handling the database tasks. You can use get() or post() function which is a shorthand of AJAX function as stated in jQuery documentation.
$.ajax({
url: url,
data: data,
success: success,
dataType: dataType
});
I've prepared a simple jsFiddle demonstrating how the task can be achieved (please note the event.preventDefault() call):
$(document).ready(function(){
$("button").click(function(e){
var URL = '';
$.get(URL,function(data){
console.log("Status: " + status);
e.preventDefault();
});
});
});
Hope that helps.

CKEditor and CkFinder work fine in PHP but don't show images, flash etc

I'm using a CKEditor along with a CKFinder. Both work fine. When I browse (or copy directly) an image (or flash) to CKEditor, it's displayed within it and inserted into the MySql database.
Aafter inserting it into MySql database, I'm trying to display it in an HTML table where it isn't displayed and the alternate text is displayed.
The image path after browsing an image through the CKFinder is something like the following.
<img alt="" src="/ckfinder/userfiles/images/1243_SS_2502.jpg" style="width: 490px; height: 618px;" />
The contents inserted into the database is as follows.
<img alt="\&quot;\&quot;" data-cke-saved-src="\"
src="\&quot;/ckfinder/userfiles/images/1243_SS_2502.jpg\&quot;" st yle=&
quot;\&quot;width:" 490px;="" height:="" 618px;\"= quot;">
Tried with htmlentities() still it doesn't work. While dealing the same with JSP using JSTL/EL, I had to do the following.
<c:out value="${str}" default="No content found." escapeXml="false"/>
escapeXml="false", where str written in EL was a java.lang.String holding the Oracle clob data after conversion.
What is the way to get around the situation in PHP? Both CKEditor and CKFinder work fine for me.
$ckeditor = new CKEditor();
$ckeditor->basePath = 'ckeditor/';
$ckeditor->config['filebrowserBrowseUrl'] = 'ckfinder/ckfinder.html';
$ckeditor->config['filebrowserImageBrowseUrl'] = 'ckfinder/ckfinder.html?type=Images';
$ckeditor->config['filebrowserFlashBrowseUrl'] = 'ckfinder/ckfinder.html?type=Flash';
$ckeditor->config['filebrowserUploadUrl'] = 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files';
$ckeditor->config['filebrowserImageUploadUrl'] = 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images';
$ckeditor->config['filebrowserFlashUploadUrl'] = 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash';
$ckeditor->editor('description', $ed_about_us);
Edit:
<?php include_once("Lock.php");?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Wagafashion</title>
<link rel="stylesheet" href="css/validationEngine.jquery.css" type="text/css"/>
<link rel="stylesheet" href="css/template.css" type="text/css"/>
<!--<script type="text/javascript" language="javascript" src="ckeditor/ckeditor.js"></script>-->
<script src="js/jquery-1.6.min.js" type="text/javascript"></script>
<script src="js/languages/jquery.validationEngine-en.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script><script>
jQuery(document).ready(function(){
// binds form submission and fields to the validation engine
jQuery("#dataForm").validationEngine();
});
</script>
<script language="javascript" type="text/javascript">
function deleteSingle(id)
{
var delId=confirm("About us with the id "+id+" is about to be deleted permanently.\n\nAttention : This action will never be undone!\n\nAre you sure...???");
return(delId==true?true:false);
}
</script>
</head>
<body>
<?php
include_once("Connection.php");
include_once("ckeditor/ckeditor.php");
$con=new Connection();
$con->get_connection();
$ed_about_us="";
$flag=-1;
$msg="";
if(isset($_POST['btnSubmit']))
{
$act=trim($_POST['param_action']);
$about_us=$_POST['cms_description'];
if($act=="add")
{
$res=$con->get_data("select count(*) as cnt from cms");
$cnt_cmt=mysql_result($res, 'cnt');
if($cnt_cmt==0)
{
$flag=$con->iud("insert into cms (about_us)values('".mysql_real_escape_string(urlencode($about_us))."')");
}
else
{
$flag=$con->iud("update cms set about_us='".mysql_real_escape_string(urlencode($about_us))."'");
}
if($flag==1)
{
$msg="Insertion done successfully.";
}
else if($flag==0)
{
$msg="Insertion failed - reason : ".mysql_errno()." : ".mysql_error();
}
}
else if($act=="edit")
{
$cms_id=$_POST['cms_id'];
$flag=$con->iud("update cms set about_us='".mysql_real_escape_string(urlencode($about_us))."' where id=".$cms_id."");
if($flag==1)
{
$msg="About us has been updated successfully.";
}
else if($flag==0)
{
$msg="Updation failed - reason : ".mysql_errno()." : ".mysql_error();
}
}
}
else if(isset($_GET['ed_id']))
{
$ed_res=$con->get_data("select about_us from cms where id=".$_GET['ed_id']."");
while($row=mysql_fetch_assoc($ed_res))
{
$ed_about_us=$row['about_us'];
}
}
else if(isset($_GET['del_id']))
{
$flag=$con->iud("update cms set about_us='' where id=".$_GET['del_id']);
if($flag==1)
{
$msg="About us been deleted successfully.";
}
else if($flag==0)
{
$msg="Can not delete - reason : ".mysql_errno()." : ".mysql_error();
}
}
else if(isset($_POST['btnDelete']))
{
$set_del=$_POST['setDel'];
$flag=$con->iud("update cms set about_us='' where id in($set_del)");
$size=sizeof(split(",", $set_del));
if($flag==1)
{
if($size==1)
{
$msg="1 row deleted.";
}
else
{
$msg=$size." rows deleted.";
}
}
else if($flag==0)
{
$msg="Can not perform deletion - reason : ".mysql_errno()." : ".mysql_error();
}
}
?>
<?php include("tamplate/Template1.php");?>
<h2>About Us</h2>
<?php include("tamplate/NewTemplate.php");?>
<?php
if($flag==1)
{
echo "<p>";
?>
<!--[if !IE]>start system messages<![endif]-->
<ul class="system_messages">
<li class="green"><span class="ico"></span><strong class="system_title"><?php echo $msg; ?></strong></li>
</ul>
<!--[if !IE]>end system messages<![endif]-->
<?php
echo "</p>";
}
else if($flag==0)
{
echo "<p>";
?>
<!--[if !IE]>start system messages<![endif]-->
<ul class="system_messages">
<li class="red"><span class="ico"></span><strong class="system_title"><?php echo $msg; ?></strong></li>
</ul>
<!--[if !IE]>end system messages<![endif]-->
<?php
echo "</p>";
}
?>
<img alt=\"\" src="/ckfinder/userfiles/images/1243_SS_2502.jpg" style=\"width: 490px; height: 618px;\" />
<!--[if !IE]>start forms<![endif]-->
<form action="<?php $_SERVER['PHP_SELF']; ?>" id="dataForm" name="dataForm" method="post" class="search_form general_form">
<!--[if !IE]>start fieldset<![endif]-->
<fieldset>
<!--[if !IE]>start forms<![endif]-->
<div class="forms">
<!--[if !IE]>start row<![endif]-->
<div class="row">
<?php
$ckeditor = new CKEditor();
$ckeditor->basePath = 'ckeditor/';
$ckeditor->config['filebrowserBrowseUrl'] = 'ckfinder/ckfinder.html';
$ckeditor->config['filebrowserImageBrowseUrl'] = 'ckfinder/ckfinder.html?type=Images';
$ckeditor->config['filebrowserFlashBrowseUrl'] = 'ckfinder/ckfinder.html?type=Flash';
$ckeditor->config['filebrowserUploadUrl'] = 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files';
$ckeditor->config['filebrowserImageUploadUrl'] = 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images';
$ckeditor->config['filebrowserFlashUploadUrl'] = 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash';
$ckeditor->editor('cms_description', urldecode($ed_about_us));
?>
<!--[if !IE]>start row<![endif]-->
<div class="row">
<div class="buttons">
<span class="button send_form_btn"><span><span>Submit</span></span><input type="submit" value="Submit" id="btnSubmit" name="btnSubmit" onclick="return validate();"></span>
</div>
</div>
<!--[if !IE]>end row<![endif]-->
</div>
</fieldset>
<!--[if !IE]>end fieldset<![endif]-->
<input type="hidden" id="param_action" name="param_action" value="
<?php
if(isset($_GET['ed_id']))
{
echo "edit";
}
else
{
echo "add";
}
?>
" />
<input type="hidden" id="cms_id" name="cms_id" value="<?php echo isset($_GET['ed_id'])?$_GET['ed_id']:"";?>" />
</form>
<?php include("tamplate/Template2.php");?>
<h2>About Us</h2>
<?php include("tamplate/NewTemplate1.php");?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" id="mainForm" name="mainForm" method="post">
<?php include("tamplate/ExtraTemplate.php");?>
<table cellpadding="0" cellspacing="0" width="100%">
<tbody>
<th style="width: 10px;">Check</th>
<th style="width: 450px;">About Us</th>
<th style="width: 10px;">Actions</th>
<?php
$get_data=$con->get_data("select id, about_us from cms order by id");
$cnt=1;$flag='';
while($data_row=mysql_fetch_assoc($get_data))
{
extract($data_row);
$cnt%2==0?$flag="second":$flag="first";
++$cnt;
echo "<tr class='$flag'>";
echo "<td><input type='checkbox' name='chk' value='$id'></td>";
echo "<td>".urldecode($about_us)."</td>";
echo "<td><div class='actions'><ul><li><a href='".$_SERVER['PHP_SELF']."?ed_id=$id' class='action2'></a></li>";
echo "<li><a href='".$_SERVER['PHP_SELF']."?del_id=$id&table_name=cms&pri=id' onclick='return deleteSingle($id);' class='action4'></a></li></ul></div></td>";
echo "</tr>";
}
?>
</tbody>
</table>
<input type='hidden' id='setDel' name='setDel'/>
<?php include("tamplate/Template3.php");?>
</form>
<?php include("tamplate/Template4.php");?>
</body>
</html>
Did you try to use html_entity_decode() to display the contents ? It will decode the encoded html for better output. Reference here
Edit
Change your query to the following
insert into cms (about_us) values ('".mysql_real_escape_string(urlecode(stripslashes($about_us)))‌​."')
When you get it from database it use
urldecode($value)
Where $value is the block you got from database.

why does my image roll over behave this way?

My image roll over works... But only one way.
function heartOver(id)
{
if(document.getElementById('heart' + id).src == 'images/heart.png');
{
parent.document.getElementById('heart' + id).src = 'images/unheart.png';
}
if(document.getElementById('heart' + id).src == 'images/unheart.png');
{
parent.document.getElementById('heart' + id).src = 'images/heart.png';
}
}
<img src="images/heart.png" id="heart'.$row['id'].'" alt="Love! width="16" height="16" border="0" onmouseover="heartOver'."('$row[id]')".'; return true;" onmouseout="heartOver'."('$row[id]')".'; return true;">
If i comment out either of the IF statements the other will work but they wont work together...
Note: Tried this with a else if no luck.
Figured it out... Duh: i have if ( ) ; No ; after if...
Put an else between them - otherwise when the first if evaluates as true, it will cause the second if to be evaluated as true also!
Here's a simplified example which also checks the assumption that the img element even exists:
var img=document.getElementById('heart' + id);
if (img)
{
if(img.src == 'images/heart.png')
{
img.src = 'images/unheart.png';
}
else if(img.src == 'images/unheart.png')
{
img.src = 'images/heart.png';
}
}
else
{
alert("Ooooh! No heart"+id+" element found!");
}
Is this correct?
<img src="images/heart.png" id="heart'.$row['id'].'" alt="Love! width="16" height="16" border="0" onmouseover="heartOver'."('$row[id]')".'; return true;" onmouseout="heartOver'."('$row[id]')".'; return true;">
Try with this:
<img src="images/heart.png" id="heart<?php echo $row['id']; ?>" alt="Love!" width="16" height="16" border="0" onmouseover="heartOver(<?php echo $row['id']; ?>)" onmouseout="heartOver(<?php echo $row['id']; ?>)">

Categories