• User

    Piccolo problema...

    Buongiorno a tutti
    ho questo pezzo di codice per aggiungere certi valori su un DB Mysql:

    
    <?php
     // creates the new record form
     // since this form is used multiple times in this file, I have made it a function that is easily reusable
     function renderForm($apiario, $lat, $long, $error)
     {
     ?>
    <!DOCTYPE html>
    <head>
    	<meta charset="utf-8" />
    </head>
    <body>
    <div class="container">
    			<div class="eight columns">
    				<h3>Elenco degli apiari</h3>
    				<div class="row">
    					<div class="twelve columns">
    						<div class="panel">
     <?php 
     // if there are any errors, display them
     if ($error != '')
     {
     echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
     }
     ?> 
     
     <form action="" method="post">
     <div>
     <label>Nome Apiario: *</label> <input type="text" class="input-text" name="apiario" value="<?php echo $apiario; ?>" /><br/>
    
     <label>Latitudine: *</label> <input type="text" class="input-text" name="lat" value="<?php echo $lat; ?>" /><br/><br>
    
     <label>Longitudine: *</label> <input type="text" class="input-text" name="long" value="<?php echo $long; ?>" /><br/><br>
     
     <p>* campi obbligatori</p>
     <input type="submit" name="submit" value="Registra" class="nice radius blue button">
     </div>
     </form> 
    </div></div>
     <!-- Included JS Files -->
    	<script src="javascripts/foundation.js"></script>
    	<script src="javascripts/app.js"></script>
     </body>
     </html>  
     <?php 
     }
      // connect to the database
     include('connect-db.php');
     
     // check if the form has been submitted. If it has, start to process the form and save it to the database
     if (isset($_POST['submit']))
     { 
     // get form data, making sure it is valid
     $apiario = mysql_real_escape_string(htmlspecialchars($_POST['apiario']));
     $lat = mysql_real_escape_string(htmlspecialchars($_POST['lat']));
     $long = mysql_real_escape_string(htmlspecialchars($_POST['long']));
    
     // check to make sure both fields are entered
     if ($apiario == '' || $lat == '' || $long == '')
     {
     // generate error message
     $error = '* Riempire i campi obbligatori';
     
     // if either field is blank, display the form again
     renderForm($apiario, $lat, $long, $error);
     }
     else
     {
     // save the data to the database
     mysql_query("INSERT apiari SET apiario='$apiario', lat='$lat', long='$long'")
     or die(mysql_error()); 
     
     // once saved, redirect back to the view page
     header("Location: index.php"); 
     }
     }
     else
     // if the form hasn't been submitted, display the form
     {
     renderForm('','','','');
     }
    ?> 
    
    

    Ho ricontrollato mille volte il codice ma ricevo questo errore:
    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 'long='testo di prova'' at line 1

    Non so più che pesci prendere !!:?
    Riuscite a darmi una mano ?

    Grazie !


  • User Attivo

    E' sbagliato la sintassi del INSERT MySQL, dai un'occhiata dev.mysql.com/doc/refman/5.5/en/insert.html.


  • User

    mysql_query($database,"INSERT INTO $db_table (campo1,campo2,...,campon)
    VALUES (valorecampo1,valorecampo2,...,valorecampon) or die(mysql_error());

    Controlla le parentesi, non sono sicuro che vadano bene