how to make a form using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rabi41
    New Member
    • Sep 2013
    • 3

    how to make a form using php

    hiiiii, i am beginer in php. i want to make a form in which four field there ie name, roll, subject and picture. name and roll must be string and numbers if not then give red box input field and save picture in a folder and also thier is two button, one is submit and other is showlist. when i click on submit button, the data is save into database and when click on showlist, the data is retrieve and show 20 rows as a table .
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    i want to make a form in which four field there ie name, roll, subject and picture. name and roll must be string and numbers if not then give red box input field and save picture in a folder and also thier is two button, one is submit and other is showlist.
    that’s an HTML question, not a PHP question.

    when i click on submit button, the data is save into database and when click on showlist, the data is retrieve and show 20 rows as a table .
    what have you tried?

    remember, we’re not a free coding service, we’re a help forum.

    Comment

    • rabi41
      New Member
      • Sep 2013
      • 3

      #3
      Code:
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
        <head>
        <meta http-equiv="content-type" content="text/html; charset=windows-1251">
        <style>
          .errText {
          	font-family: Arial;
          	font-size: 10px;
          	color: #CC0000;
          	text-decoration: none;
          	font-weight: normal;
          }
      	a{
      		text-decoration: none;
      	}
        </style>
        <title></title>
        </head>
        <?php
        
      	$con=mysqli_connect("localhost","root","");
      	if (mysqli_connect_errno())
      	{
      		echo "Failed to connect to MySQL: " . mysqli_connect_error();
      	}
      
      	mysql_select_db("test") OR DIE ("Unable to select db".mysql_error());
      	
      	$sql = "CREATE TABLE details (
      		pid int primary key not null auto_increment,
      		name CHAR(30),
      		roll INT(10).
      		subject VATCHAR(50),
      		picture longblob)";
      	
      		
            $errName     = "";
            $errroll  = "";
      	  
      	  $name=$_POST["name"];
      	  $roll=$_POST["roll"];
      	  $subject=$_POST["subject"];
      	  $picture=$_POST["picture"];
      		
              
            if($_POST["ac"]=="login"){
              
              if(preg_match("/^[A-Z][a-zA-Z -]+$/", $name) === 0)
                $errName = '<p class="errText">Name must be from letters, dashes, spaces and must not start with dash</p>';
              
              if(preg_match("/^[0-9]+$/", $roll) === 0)
                $errroll = '<p class="errText">roll must be only numbers </p>';
                      
            }
      
      		if (!isset($_POST['submit'])) {
      		
      		$allow = array("jpg", "jpeg", "png");
      
      		$todir = 'uploads/';
      
      		if ( !!$_FILES['file']['tmp_name'] )
      		{
      			$ext = explode('.', strtolower( $_FILES['file']['picture']) );
      
      			if ( in_array( $ext, $allow) )
      			{
      				if ( move_uploaded_file( $_FILES['file']['tmp_name'], $todir . basename($_FILES['file']['picture'] ) ) )
      				{
      					echo "Upload: " . $_FILES["file"]["picture"] . "<br>";
      					echo "Type: " . $_FILES["file"]["type"] . "<br>";
      					echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
      					echo "Stored in: " . $_FILES["file"]["tmp_name"];
      				}
      			}
      			else
      			{
      				echo "Invalid file";
      			}
      		}
      
      		$result="INSERT INTO details (name,roll,subject,picture) VALUES ('$name', '$roll', '$subject', '$picture')";
      		if (!mysqli_query($con,$result))
      		{
      			echo "there was some error";
      		}
        ?>
        <body>
        <center>
        <form name="main" action="<?php $PHP_SELF ?>" method="POST">
          <input type="hidden" name="ac" value="login">
          <table width="500" border="0" cellpadding="4" cellspacing="0" bordercolor="#000000" bgcolor="#EDEFF1">
            <tr align="center" bgcolor="#FD9003">
              <td colspan="2" bgcolor="#A6B39D">Registration Form</td>
            </tr>
            <tr>
              <td>Name:</td>
              <td>
                <input name="name" type="text" size="50" maxlength="100" value="<?php echo $_POST['name']; ?>"/>
                <?php  if(isset($errName)) echo $errName; ?>
              </td>
            </tr>
            <tr>
              <td>Roll:</td>
              <td>
                <input name="roll" type="text" size="50" maxlength="10" value="<?php echo $_POST['roll']; ?>"/>
                <?php  if(isset($errroll)) echo $errroll; ?>
              </td>
            </tr>
            <tr>
              <td>Subject:</td>
              <td>
                <input name="subject" type="text" size="50" />
              </td>
            </tr>
            <tr>
              <td>Picture:</td>
              <td>
                <input name="picture" type="file" />
              </td>
            </tr>      
              <td>&nbsp;</td>
              <td><input type="submit" name="Submit" value="Submit"></td>
      		<td><a href="test2.php"><input type="button" name="Submit" value="Show List" /></a></td>
            </tr>
          </table>
        </form>
        </center>
        </body>
        <?php
      	}
        ?>
      </html>

      What's wrong in above code
      Last edited by Dormilich; Sep 20 '13, 09:58 AM. Reason: Please use [CODE] [/CODE] tags when posting code.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        What's wrong in above code
        you’re mixing mysql and mysqli.

        Comment

        Working...