Hello, I have written this code for adding the user details into the database, <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_student1", $con); $firstname=$_POST['firstname']; $lastname=$_POST['lastname']; $degree=$_POST['degree']; if($_POST['firstname']=='$firstname') { echo " The details already exists"; } else { $sql="INSERT INTO person (firstname, lastname, degree) VALUES ('$firstname','$lastname','$degree')"; mysql_query($sql,$con); $user=$_POST['firstname']; $first_chr=substr($user,3); $year=date('y'); $rand_id=$first_chr .$year; $sql1="INSERT INTO person(id) VALUES ('$rand_id')"; mysql_query($sql1,$con); echo "Your details are added in the database and you can use this id, <b>$rand_id</b>, for furture references"; } mysql_close($con) ?> Code (markup): I have three fields in the database firstname, lastname, degree I am struck on how to write the code to display a message, "the details are already exists" if the user enter the same details. I need help as soon as possible. Thanks.
<?php if (!($con = mysql_connect("localhost","root",""))) die('Could not connect: ' . mysql_error()); else mysql_select_db( "my_student1", $con ); $firstname = stripslashes( trim( $_POST['firstname'] ) ); $lastname = stripslashes( trim( $_POST['lastname'] ) ); $degree = stripslashes( trim( $_POST['degree'] ) ); if( !@mysql_num_rows( mysql_query( "SELECT * FROM `person` WHERE firstname = '$firstname' AND lastname = '$lastname' AND degree = '$degree'" ) ) ) { $id = substr( md5( time() * strlen( $firstname ) ), 0, 8 ); if( mysql_query( "INSERT INTO person (id, firstname, lastname, degree) VALUES ('$id', '$firstname','$lastname','$degree')",$con) ) { echo "Your details are added in the database and you can use this id, <b>$id</b>, for furture references"; } else { echo "Your details could not be inserted at this time, please contact support"; } } else { echo "Your details exist in the database."; } mysql_close($con); ?> PHP: try that
Hello KrakJoe, I got the desired results using your code, thanks for that. I want to search the user details using the '$id' as search string from the database. How to write the code for that. Thanks.
<? if( $_POST ) : if (!($con = mysql_connect("localhost","root",""))): die('Could not connect: ' . mysql_error()); else: mysql_select_db( "my_student1", $con ); endif; $result = mysql_query( "SELECT * FROM `person` WHERE id = '$_POST[id]' LIMIT 1", $con ); if( !mysql_num_rows( $result ) ): echo "Records not found for that ID"; else: $assoc = mysql_fetch_assoc( $result ); printf( "Records found<br/>\n". "Firstname : %s<br/>\n". "Lastname : %s<br/>\n". "Degree : %s<br/>\n", $assoc['firstname'], $assoc['lastname'], $assoc['degree'] ); endif; endif; ?> <center> <form action="" method="post" > Please enter an id : <input type="text" name="id" /> <input type="submit" value="Search"> </form> </center> PHP:
Hello Krak Joe, Your coding skills are simply superb, I need to update the database whenever user requests, for example, if the user wants to change his last name or first name using $id as reference. How to write the code for this logic.
<? if( $_POST ): if (!($con = mysql_connect("localhost","root",""))): die('Could not connect: ' . mysql_error()); else: mysql_select_db( "my_student1", $con ); endif; $result = mysql_query( "UPDATE `person` SET firstname = '$_POST[first]', lastname = '$_POST[last]', degree = '$_POST[degree]' WHERE id = '$_POST[id]' LIMIT 1", $con ); if( mysql_num_rows( $result ) ): echo "Database updated<br/>"; else: echo "ID not found<br/>"; endif; endif; ?> <form action="" method="post"> Enter ID : <input type="text" name="id" /><br /> Enter New Firstname : <input type="text" name="first" /><br/ > Enter New Lastname : <input type="text" name="last" /><br /> Enter New Degree : <input type="text" name="degree" /><br /> <input type="submit" value="Update" /> </form> PHP: That code is not safe, anyone could change anything, but that's how you would update the table with form results .....