How to dynamically create a PHP class name on the fly?

How do I create a PHP form for creating new row and inserting data into it?

  • Here is a mockup of what I want to do. It's pretty straightforward. On clicking submit, it creates a new row (like into USERS table which already exists), and enters the four data into their respective columns. here is the image I already know how to run a query     $query = "INSERT INTO myTable           (fname, lname, user, pass)           VALUES           ('John', 'Smith', 'johnsmith', 'mypassword0')"; However, what I want to do is to make each VALUE an empty space, so that a user can type something in, click SUBMIT button, and it will create the new table row and enter the data into it. p.s. I am already connected to the database and able to successfully pull data from it. So I just need to know how to create the form; I don't need to know how to connect to the database because I am already connected. p.s.s. Here is the basic form layout. Whatever is missing is what I need to know. Needs to be inline php and on submit stays on same page. <?php if(isset($_POST['submit'])){     // NEED TO KNOW THIS INFO      }else{?> <form method="post">     <form>         <div><strong>First Name:</strong><span class="errortext">*</span></div>         <div>          <input id="fname" name="fname" tabindex="1"   type="text" />         </div>         <div><strong>Last Name:</strong><span class="errortext">*</span></div>         <div>             <input id="lname" name="lname" tabindex="3"  type="text" />         </div>         <div><strong>User:</strong><span class="errortext">*</span></div>         <div>             <input id="user" name="user"  type="text" />         </div>         <div><strong>pass:</strong><span class="errortext">*</span></div>         <div>             <input id="pass" name="pass"  type="text" />         </div>         <div><input value="submit" /></div>              </form> <?php }?>

  • Answer:

    As per the http://us2.php.net/manual/en/mysqli.quickstart.prepared-statements.php documentation: <?php $mysqli = new mysqli("example.com", "user", "password", "database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } /* Prepare statement */ if (!($stmt = $mysqli->prepare("INSERT INTO mytable (fname, lname, user, pass) VALUES (?, ?, ?, ?)"))) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; } /* Bind parameters */ $fname = $_POST['fname']; $lname = $_POST['lname']; $user = $_POST['user']; $salt = '$6$saltstring$'; $hash = crypt($_POST['pass'], $salt); if (!$stmt->bind_param("ssss", $fname, $lname, $user, $hash)) { echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; } /* Execute statement */ if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; } /* Explicit close */ $stmt->close(); ?> http://DevShed.com has lots of web development tutorials. I would start there (or get a good MySQL & PHP development book, e.g. from O'Reilly) and then look up (or search for) functions you're unfamiliar with on PHP.net's online documentation and http://MySQL.com.

Calvin Huang at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.