2 Replies - 2604 Views - Last Post: 10 May 2012 - 07:54 AM

#1 PF2G  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-November 11

Update jquery sortable DB

Posted 10 May 2012 - 05:20 AM

Hi,
i'm working on this website where i search for a product and it lists the website and then i made jquery sortable so the client can chane the order, but i'm having difficults doing the Update can you help me please?

Here's the code i have so far:

<?php 
	require_once('Connections/futura.php'); 
?>

<?PHP
	$prod=$_GET['q2'];
	
	$query_prod="SELECT * FROM produtos WHERE nome LIKE '%".$prod."%'";
    $produt=mysql_query($query_prod, $futura);
    $count=mysql_num_rows($produt);   
	  
    
	$prod_img="SELECT * FROM produtos WHERE nome LIKE '%". $prod ."%' ORDER BY ordem ASC";
	$image=mysql_query($prod_img, $futura);
?>
<!----------------------------------------------------------------------------------------------------------------------->

<!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" />
<link href="styles/style.css" rel="stylesheet" />
<title>Plásticos Futura - Pesquisa</title>
<link rel="shortcut icon" type="image/x-icon" href="imagens/plasticos.ico" />
<script type="text/javascript" src="js/java.js"></script>

<!-------------------------------------------------------------------------------------->
<link rel="stylesheet" href="jquery/themes/base/jquery.ui.all.css">

<script src="jquery/jquery-1.7.2.js"></script>
<script src="jquery/ui/jquery.ui.core.js"></script>
<script src="jquery/ui/jquery.ui.widget.js"></script>
<script src="jquery/ui/jquery.ui.mouse.js"></script>
<script src="jquery/ui/jquery.ui.sortable.js"></script>
<link rel="stylesheet" href="jquery/demos.css">


<style type="text/css">
		#sortable 
		{
			list-style-type: none; 
			margin: 0; 
			padding: 0; 
			width: 100%; 
		}
		
		
		#sortable tr 
		{ 
			font-size:12px;
			margin: 0 3px 3px 3px; 
			padding: 0.4em; 
			padding-left: 1.5em; 
			height: 20px; 
		}
</style>

<script type="text/javascript">    
	$(function() 
	{        
		$("#sortable").sortable(
		{
			placeholder: 'ui-state-highlight',            
			stop: function(i) 
			{
				placeholder: 'ui-state-highlight'                
				$.ajax(
				{     
					type: "GET",                 
					data: $("#sortable").sortable("serialize")
				});            
			}        
		});        
		$("#sortable").disableSelection();    
	});
</script>
<!---------------------------------------------------------------------------------------------------->
<?php 
	//UPDATE
?>
</head>
<!---------------------------------------------------------------------------------------------------->
<body>
<table width="1200" border="0" align="center" cellpadding="0" cellspacing="0" style="background-image:url(imagens/fundo5.jpg);background-repeat:repeat-y;">
<tr>
<td>
<table width="1200" border="0" align="center" cellpadding="0" cellspacing="0" style="background-image:url(imagens/fundo3.jpg); background-repeat:no-repeat;" height="713" >
  <tr>
    <td height="60" valign="bottom"><?php include("topo.php"); ?></td>
  </tr>
  <tr>
    <td height="613" valign="top"><table width="1200" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td valign="top"><table width="260" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td align="right"><a href="index.php" title="Voltar à homepage"><img src="imagens/plasticos-futura.png" border="0" /></a></td>
          </tr>
          <tr>
            <td height="40"></td>
          </tr>
          <tr>
            <td><table width="250" border="0" align="right" cellpadding="0" cellspacing="0">
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='index.php'"><a href="index.php" title="Home - Plásticos Futura" class="menu_text">Home</a></td>
              </tr>
              <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='empresa.php'"><a href="empresa.php" class="menu_text" title="Empresa - Plásticos Futura">Empresa</a></td>
              </tr>
              <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='produtos.php'"><a href="produtos.php" class="menu_text" title="Produtos - Plásticos Futura">Produtos</a></td>
              </tr>
              <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='noticias.php'"><a href="noticias.php" class="menu_text"  title="Notícias - Plásticos Futura">Notícias</a></td>
              </tr>
               <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
			  <tr>
                  <td width="30"></td>
                  <td height="26" class="menu" onclick="window.location='oportunidades-de-negocio.php'"><a href="oportunidades-de-negocio.php" class="menu_text"  title="Oportunidades de Negócio - Plásticos Futura">Oportun. Negócio</a></td>
                </tr>
                <tr>
                  <td height="2"></td>
                  <td height="2"></td>
                </tr>
			  <tr>
                  <td width="30"></td>
                  <td height="26" class="menu" onclick="window.location='recrutamento.php'"><a href="recrutamento.php" class="menu_text"  title="Recrutamento - Plásticos Futura">Recrutamento</a></td>
                </tr>
                <tr>
                  <td height="2"></td>
                  <td height="2"></td>
                </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='contactos.php'"><a href="contactos.php" class="menu_text"  title="Contactos - Plásticos Futura">Contactos</a></td>
              </tr>
            </table></td>
          </tr>
        </table></td>


<!----------------------------------------------------------------------------------------------------------------------->	
		 <td width="638" valign="top" class="texto_pretobig2"><div class="bomfundo"><strong>RESULTADOS DA PESQUISA::</strong><br />
		<br/>
		
<?PHP
		if($count==0)
		{
?>

		<strong>Não foram encontrados resultados!</strong>
		
<?PHP
		}	
		else
		{ 
			echo 'Encontrado(s) '.$count.' resultado(s) de pesquisa com a palavra "'. $prod .'":';
?>
			<br/>
			<br/>
		<form method="POST" action="">
		         <table id="sortable" width="570" border="0" cellspacing="0" cellpadding="0" >
					
                <?PHP
                        $i=0;
                        while ($linha = mysql_fetch_array($image))
                        {
				?>
							
				<?PHP
							if ($i==0)
                            {
                                echo '<tr>';
                            }
				?>
						<tbody>
				<?PHP
							//MATERIAL
									$query_categ_list = "SELECT * FROM materiais WHERE id_material='".$linha['id_material']."'";
									$categ_list = mysql_query($query_categ_list, $futura) or die(mysql_error());
									$row_categ_list = mysql_fetch_assoc($categ_list);
									$totalRows_categ_list = mysql_num_rows($categ_list);
				?> 
				<td colspan="2" class="div_topo"><a name="cat<?php echo $row_categ_list['id_material']; ?>"></a>&nbsp; <strong>PRODUTOS :: <span class="texto2"><?php echo $row_categ_list['material']; ?></span></strong></td>
							
				<?PHP
								//IMAGEM
								$sel_img="SELECT * FROM  produtos_img WHERE id_produto = '".$linha['id_produto']."'";
								$img2=mysql_query($sel_img, $futura);
								$img = mysql_fetch_assoc($img2);
				?>
								<tr>
							    <td width="160" height="95" valign="top"><a href="ver_produto.php?id_produto=<?php echo $linha['id_produto']; ?>&amp;id_categoria=<?php echo $linha['id_material']; ?>"><?php if($img['imagem']!="") { ?><img src="http://www.plasticos-futura.com/timthumb.php?src=http://www.plasticos-futura.com/produtos/<?php echo $img['imagem']; ?>&w=134px&h=98px&zc=1&q=100" width="134" height="98" style="border:1px solid #cccccc;" /><?php } else { ?><img src="imagens/futura.jpg" width="134" height="98" style="border:1px solid #cccccc;" /><?php } ?></a></td>
								
								<td>
									<table>
										<tr >
											<td align="left">
												<strong>DESCRIÇÃO: </strong><?PHP echo $linha['nome'];?>
											</td>
										</tr>
										
										<tr>
											<td align="left">
												<strong>REFERÊNCIA: </strong><?PHP echo $linha['referencia']; ?>
											</td>
										</tr>
									</table>
								</td>
								</tr>
							</tbody>
						<?PHP
							$i=$i+1;
                        }
                ?>
				</table>
				<br/>
			<input type="submit" name="do_submit" value="Submit Sortation" class="button" />
		</form>
	<?PHP
		}
	?>
<!----------------------------------------------------------------------------------------------------------------------->
 </div></td>
        <td width="178"></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="40"><?php include("rodape.php"); ?></td>
  </tr>
</table>
</td>
  </tr>
  </table>
</body>
</html>



I'm sorry: *change the order

Is This A Good Question/Topic? 0
  • +

Replies To: Update jquery sortable DB

#2 PF2G  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-November 11

Re: Update jquery sortable DB

Posted 10 May 2012 - 06:43 AM

What is wrong with this?

It doesn't give me any error but it doesn't update...

<?php 
	require_once('Connections/futura.php'); 
?>

<?PHP
	$prod=$_GET['q2'];
	
	$query_prod="SELECT * FROM produtos WHERE nome LIKE '%".$prod."%'";
    $produt=mysql_query($query_prod, $futura);
    $count=mysql_num_rows($produt);   
	  
    
	$prod_img="SELECT * FROM produtos WHERE nome LIKE '%". $prod ."%' ORDER BY ordem ASC";
	$image=mysql_query($prod_img, $futura);
?>
<!----------------------------------------------------------------------------------------------------------------------->

<!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" />
<link href="styles/style.css" rel="stylesheet" />
<title>Plásticos Futura - Pesquisa</title>
<link rel="shortcut icon" type="image/x-icon" href="imagens/plasticos.ico" />
<script type="text/javascript" src="js/java.js"></script>

<!-------------------------------------------------------------------------------------->
<link rel="stylesheet" href="jquery/themes/base/jquery.ui.all.css">

<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script type="text/javascript" src="jquery-ui-1.5.1.min.js"></script>

<script src="jquery/jquery-1.7.2.js"></script>
<script src="jquery/ui/jquery.ui.core.js"></script>
<script src="jquery/ui/jquery.ui.widget.js"></script>
<script src="jquery/ui/jquery.ui.mouse.js"></script>
<script src="jquery/ui/jquery.ui.sortable.js"></script>
<link rel="stylesheet" href="jquery/demos.css">


<style type="text/css">
		#sortable 
		{
			list-style-type: none; 
			margin: 0; 
			padding: 0; 
			width: 100%; 
		}
		
		
		#sortable tr 
		{ 
			font-size:12px;
			margin: 0 3px 3px 3px; 
			padding: 0.4em; 
			padding-left: 1.5em; 
			height: 20px; 
		}
</style>

<script type="text/javascript">    
	$(function() 
	{        
		$("#sortable").sortable(
		{
			placeholder: 'ui-state-highlight',            
			stop: function(i) 
			{
				placeholder: 'ui-state-highlight'                
				$.ajax(
				{     
					type: "GET",                 
					data: $("#sortable").sortable("serialize")
				});            
			}        
		});        
		$("#sortable").disableSelection();    
	});
</script>
<!---------------------------------------------------------------------------------------------------->
<?php 
	// handle POST
	if (isset($_POST['reorder']))
	{
		// use $i to increment the weight
		$i=1;
		// loop through post array in the order it was submitted
		foreach ($_POST['prod'] as $prod_id) 
		{
			// update the row
			$result = "UPDATE produtos SET ordem=". $i . " WHERE id_produto=". mysql_real_escape_string($prod_id);
			$update = mysql_query($result, $futura);
			// increment weight to make the next fruit heavier
			$i++;
		}
	}
?>
</head>
<!---------------------------------------------------------------------------------------------------->
<body>
<table width="1200" border="0" align="center" cellpadding="0" cellspacing="0" style="background-image:url(imagens/fundo5.jpg);background-repeat:repeat-y;">
<tr>
<td>
<table width="1200" border="0" align="center" cellpadding="0" cellspacing="0" style="background-image:url(imagens/fundo3.jpg); background-repeat:no-repeat;" height="713" >
  <tr>
    <td height="60" valign="bottom"><?php include("topo.php"); ?></td>
  </tr>
  <tr>
    <td height="613" valign="top"><table width="1200" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td valign="top"><table width="260" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td align="right"><a href="index.php" title="Voltar à homepage"><img src="imagens/plasticos-futura.png" border="0" /></a></td>
          </tr>
          <tr>
            <td height="40"></td>
          </tr>
          <tr>
            <td><table width="250" border="0" align="right" cellpadding="0" cellspacing="0">
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='index.php'"><a href="index.php" title="Home - Plásticos Futura" class="menu_text">Home</a></td>
              </tr>
              <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='empresa.php'"><a href="empresa.php" class="menu_text" title="Empresa - Plásticos Futura">Empresa</a></td>
              </tr>
              <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='produtos.php'"><a href="produtos.php" class="menu_text" title="Produtos - Plásticos Futura">Produtos</a></td>
              </tr>
              <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='noticias.php'"><a href="noticias.php" class="menu_text"  title="Notícias - Plásticos Futura">Notícias</a></td>
              </tr>
               <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
			  <tr>
                  <td width="30"></td>
                  <td height="26" class="menu" onclick="window.location='oportunidades-de-negocio.php'"><a href="oportunidades-de-negocio.php" class="menu_text"  title="Oportunidades de Negócio - Plásticos Futura">Oportun. Negócio</a></td>
                </tr>
                <tr>
                  <td height="2"></td>
                  <td height="2"></td>
                </tr>
			  <tr>
                  <td width="30"></td>
                  <td height="26" class="menu" onclick="window.location='recrutamento.php'"><a href="recrutamento.php" class="menu_text"  title="Recrutamento - Plásticos Futura">Recrutamento</a></td>
                </tr>
                <tr>
                  <td height="2"></td>
                  <td height="2"></td>
                </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='contactos.php'"><a href="contactos.php" class="menu_text"  title="Contactos - Plásticos Futura">Contactos</a></td>
              </tr>
            </table></td>
          </tr>
        </table></td>


<!----------------------------------------------------------------------------------------------------------------------->	
		 <td width="638" valign="top" class="texto_pretobig2"><div class="bomfundo"><strong>RESULTADOS DA PESQUISA::</strong><br />
		<br/>
		
<?PHP
		if($count==0)
		{
?>

		<strong>Não foram encontrados resultados!</strong>
		
<?PHP
		}	
		else
		{ 
			echo 'Encontrado(s) '.$count.' resultado(s) de pesquisa com a palavra "'. $prod .'":';
?>
			<br/>
			<br/>
		<form method="POST" action="">
		         <table id="sortable" width="570" border="0" cellspacing="0" cellpadding="0" >
					
                <?PHP
                        $i=0;
                        while ($linha = mysql_fetch_array($image))
                        {
				?>
							
				<?PHP
							if ($i==0)
                            {
                                echo '<tr>';
                            }
				?>
						<tbody>
						<input type="hidden" name="prod[]" value="'. $linha['id'] .'" />
				<?PHP
							//MATERIAL
									$query_categ_list = "SELECT * FROM materiais WHERE id_material='".$linha['id_material']."'";
									$categ_list = mysql_query($query_categ_list, $futura) or die(mysql_error());
									$row_categ_list = mysql_fetch_assoc($categ_list);
									$totalRows_categ_list = mysql_num_rows($categ_list);
				?> 
				<td colspan="2" class="div_topo"><a name="cat<?php echo $row_categ_list['id_material']; ?>"></a>&nbsp; <strong>PRODUTOS :: <span class="texto2"><?php echo $row_categ_list['material']; ?></span></strong></td>
							
				<?PHP
								//IMAGEM
								$sel_img="SELECT * FROM  produtos_img WHERE id_produto = '".$linha['id_produto']."'";
								$img2=mysql_query($sel_img, $futura);
								$img = mysql_fetch_assoc($img2);
				?>
								<tr>
							    <td width="160" height="95" valign="top"><a href="ver_produto.php?id_produto=<?php echo $linha['id_produto']; ?>&amp;id_categoria=<?php echo $linha['id_material']; ?>"><?php if($img['imagem']!="") { ?><img src="http://www.plasticos-futura.com/timthumb.php?src=http://www.plasticos-futura.com/produtos/<?php echo $img['imagem']; ?>&w=134px&h=98px&zc=1&q=100" width="134" height="98" style="border:1px solid #cccccc;" /><?php } else { ?><img src="imagens/futura.jpg" width="134" height="98" style="border:1px solid #cccccc;" /><?php } ?></a></td>
								
								<td>
									<table>
										<tr >
											<td align="left">
												<strong>DESCRIÇÃO: </strong><?PHP echo $linha['nome'];?>
											</td>
										</tr>
										
										<tr>
											<td align="left">
												<strong>REFERÊNCIA: </strong><?PHP echo $linha['referencia']; ?>
											</td>
										</tr>
									</table>
								</td>
								</tr>
							</tbody>
						<?PHP
							$i=$i+1;
                        }
                ?>
				</table>
				<br/>
			<input type="submit" name="reorder" value="Submit Sortation" class="button" />
		</form>
	<?PHP
		}
	?>
<!----------------------------------------------------------------------------------------------------------------------->
 </div></td>
        <td width="178"></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="40"><?php include("rodape.php"); ?></td>
  </tr>
</table>
</td>
  </tr>
  </table>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6087
  • View blog
  • Posts: 23,597
  • Joined: 23-August 08

Re: Update jquery sortable DB

Posted 10 May 2012 - 07:54 AM

You need to learn MySQL error handling. You -- like so many n00bs -- never bother to check the success or failure of a database query, ESPECIALLY one which is constructed using user-inputted values.

$result = "UPDATE produtos SET ordem=". $i . " WHERE id_produto=". mysql_real_escape_string($prod_id);


If id_produto is a string, then it needs to be in single quotes within the query.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1