Crud System Semplice
-
Ciao a tutti,navigando in rete ho trovato un tutorial ben fatto riguardo ad un semplice sistema crud (create-read-update-delete) in php/mysql, il sistema funziona correttamente, soltanto che non riesco ad aggiungere un nuovo campo..
Potete vedere il tutorial in lingua inglese qui : startutorial.com/articles/view/php-crud-tutorial-part-1/
Potete scaricarvi i sorgenti dello script qui : tartutorial.com/downloads/crud.zip
Attualmente lo script ha i seguenti campi:Nome-Email-Mobile.
voglio aggiungere un campo textarea "messaggio".
Ho creato i campi da phpmyadmin:CREATE TABLE `customers` (`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,`name` VARCHAR( 100 ) NOT NULL ,`email` VARCHAR( 100 ) NOT NULL ,`messaggio` VARCHAR( 100 ) NOT NULL , `mobile` VARCHAR( 100 ) NOT NULL) ENGINE = INNODB;Lo script è formato da 4 pagine: create.php delete.php update.php read.php
Pagina create.php è composta da due parti la parte php:
<?php require 'database.php'; if ( !empty($_POST)) { // keep track validation errors $nameError = null; $emailError = null; $mobileError = null; // keep track post values $name = $_POST['name']; $email = $_POST['email']; $mobile = $_POST['mobile']; // validate input $valid = true; if (empty($name)) { $nameError = 'Please enter Name'; $valid = false; } if (empty($email)) { $emailError = 'Please enter Email Address'; $valid = false; } else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) { $emailError = 'Please enter a valid Email Address'; $valid = false; } if (empty($mobile)) { $mobileError = 'Please enter Mobile Number'; $valid = false; } // insert data if ($valid) { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO customers (name,email,mobile) values(?, ?, ?)"; $q = $pdo->prepare($sql); $q->execute(array($name,$email,$mobile)); Database::disconnect(); header("Location: index.php"); } } ?>e la parte html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link href="css/bootstrap.min.css" rel="stylesheet"> <script src="js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="span10 offset1"> <div class="row"> <h3>Create a Customer</h3> </div> <form class="form-horizontal" action="create.php" method="post"> <div class="control-group <?php echo !empty($nameError)?'error':'';?>"> <label class="control-label">Name</label> <div class="controls"> <input name="name" type="text" placeholder="Name" value="<?php echo !empty($name)?$name:'';?>"> <?php if (!empty($nameError)): ?> <span class="help-inline"><?php echo $nameError;?></span> <?php endif; ?> </div> </div> <div class="control-group <?php echo !empty($emailError)?'error':'';?>"> <label class="control-label">Email Address</label> <div class="controls"> <input name="email" type="text" placeholder="Email Address" value="<?php echo !empty($email)?$email:'';?>"> <?php if (!empty($emailError)): ?> <span class="help-inline"><?php echo $emailError;?></span> <?php endif;?> </div> </div> <div class="control-group <?php echo !empty($mobileError)?'error':'';?>"> <label class="control-label">Mobile Number</label> <div class="controls"> <input name="mobile" type="text" placeholder="Mobile Number" value="<?php echo !empty($mobile)?$mobile:'';?>"> <?php if (!empty($mobileError)): ?> <span class="help-inline"><?php echo $mobileError;?></span> <?php endif;?> </div> </div> <div class="form-actions"> <button type="submit" class="btn btn-success">Crea</button> <a class="btn" href="index.php">Indietro</a> </div> </form> </div> </div> <!-- /container --> </body> </html>ho modificato la pagina create.php aggiungendo la funzione di volta in volta:
<?php require 'database.php'; if ( !empty($_POST)) { // keep track validation errors $nameError = null; $emailError = null; $mobileError = null; $messaggioError = null; // keep track post values $name = $_POST['name']; $email = $_POST['email']; $mobile = $_POST['mobile']; $messaggio= $_POST['messaggio']; // validate input $valid = true; if (empty($name)) { $nameError = 'Please enter Name'; $valid = false; } if (empty($email)) { $emailError = 'Please enter Email Address'; $valid = false; } else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) { $emailError = 'Please enter a valid Email Address'; $valid = false; } if (empty($mobile)) { $mobileError = 'Please enter Mobile Number'; $valid = false; } if (empty($messaggio)) { $messaggioError = 'scrivi qualcosa'; $valid = false; } // insert data if ($valid) { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO customers (name,email,mobile,messaggio) values(?, ?, ?, ?)"; $q = $pdo->prepare($sql); $q->execute(array($name,$email,$mobile)); Database::disconnect(); header("Location: index.php"); } }?><!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <link href="css/bootstrap.min.css" rel="stylesheet"> <script src="js/bootstrap.min.js"></script></head> <body> <div class="container"> <div class="span10 offset1"> <div class="row"> <h3>Create a Customer</h3> </div> <form class="form-horizontal" action="create.php" method="post"> <div class="control-group <?php echo !empty($nameError)?'error':'';?>"> <label class="control-label">Name</label> <div class="controls"> <input name="name" type="text" placeholder="Name" value="<?php echo !empty($name)?$name:'';?>"> <?php if (!empty($nameError)): ?> <span class="help-inline"><?php echo $nameError;?></span> <?php endif; ?> </div> </div> <div class="control-group <?php echo !empty($emailError)?'error':'';?>"> <label class="control-label">Email Address</label> <div class="controls"> <input name="email" type="text" placeholder="Email Address" value="<?php echo !empty($email)?$email:'';?>"> <?php if (!empty($emailError)): ?> <span class="help-inline"><?php echo $emailError;?></span> <?php endif;?> </div> </div> <div class="control-group <?php echo !empty($mobileError)?'error':'';?>"> <label class="control-label">Mobile Number</label> <div class="controls"> <input name="mobile" type="text" placeholder="Mobile Number" value="<?php echo !empty($mobile)?$mobile:'';?>"> <?php if (!empty($mobileError)): ?> <span class="help-inline"><?php echo $mobileError;?></span> <?php endif;?> </div> </div> <div class="control-group <?php echo !empty($messaggioError)?'error':'';?>"> <label class="control-label">Messaggio</label> <div class="controls"> <input name="mobile" type="text" placeholder="Messaggio" value="<?php echo !empty($messaggio)?$messaggio:'';?>"> <?php if (!empty($messaggioError)): ?> <span class="help-inline"><?php echo $messaggioError;?></span> <?php endif;?> </div> </div> <div class="form-actions"> <button type="submit" class="btn btn-success">Create</button> <a class="btn" href="index.php">Back</a> </div> </form> </div> </div> <!-- /container --> </body></html>Non so come devo procedere, in realtà da dove iniziare!
Non mi funziona, il controllo sui campi è attivo ma non mi salva i dati, cliccando su "crea" vedo la pagina bianca.
Chi mi può aiutare?
Grazie
Ciao! Sembra che tu sia interessato a questa conversazione, ma non hai ancora un account.
Stanco di dover scorrere gli stessi post a ogni visita? Quando registri un account, tornerai sempre esattamente dove eri rimasto e potrai scegliere di essere avvisato delle nuove risposte (tramite email o notifica push). Potrai anche salvare segnalibri e votare i post per mostrare il tuo apprezzamento agli altri membri della comunità.
Con il tuo contributo, questo post potrebbe essere ancora migliore 💗
Registrati Accedi