Creating album for mysql and php online photo album -
i creating mysql backed online photo album , using php access mysql database, having trouble trying create form lets users create new album, far have following code:
<h1>my photo collection</h1> <h3>add, search , browse through our collection!</h3> <section id="collection"> <h4>create album</h4> <form method='post'> <?php require_once 'config.php'; $mysqli = new mysqli(db_host, db_user, db_password, db_name); if(isset($_post['title'])){ $name = $_post['title']; if(empty($name)){ echo "please enter album name <br/>"; } else{ $mysqli->query("insert albums values('', '$title')"); echo "album created <br/>"; } } ?> album name: <input type="text" name="title" /> <input type="submit" value="create" /> </form> </section>
my schema ' albums' table is: title, date_created, date_modified (time stamp), albumid (primary key)
further questions: how allow user enter date no form (and make sure in correct format), how date_modified timestamp work (do need put in form?)
i using mysqli , not mysql in php queries. far, code returns "undefined object 'title'" can me , figure out i'm doing wrong code?
thank you!
currently, $title
undefined, assigned $_post['title']
$name
. , since you're using mysqli api, use prepared statements instead.
as date, use now()
<?php require_once 'config.php'; $mysqli = new mysqli(db_host, db_user, db_password, db_name); if(isset($_post['submit'])) { if(empty($_post['title'])) { echo 'please enter album name <br/>'; } else { $name = $_post['title']; $insert = $mysqli->prepare('insert albums (title, date_created, date_modified) values(?, now(), now())') or die($mysqli->error); $insert->bind_param('s', $name); if($insert->execute()) { echo 'album created <br/>'; } else { echo $mysqli->error; } } } ?> <h1>my photo collection</h1> <h3>add, search , browse through our collection!</h3> <section id="collection"> <h4>create album</h4> <form method='post'> album name: <input type="text" name="title" /> <input type="submit" name="submit" value="create" /> </form> </section>
Comments
Post a Comment