Hi,

Im just starting with PHP, although im fluent in alot of other techs.

I'm trying to find a simple example of adding/change/deleting/displaying a mysql record through a web page.

Just plain simple example...

Its now 2:30am, ive been searching all night for an example with no luck, i cant even see straight!

Im sure every newbie would love to have this as an example, someone MUST have something like this.

thanks
--Karl

    Variables from query string

    $FirstName = $_GET['firstname'];
    $LastName = $_GET['lastname'];
    $NewFirstName = $_GET['newfirstname'];
    

    Escape the variables

    if (!(get_magic_quotes_gpc())) {
    	$FirstName = addslashes($FirstName);
    	$LastName = addslashes($LastName);
    	$NewFirstName = addslashes($NewFirstName);
    }
    

    Connect to database

    $db_addr = "localhost";
    $db_user = "username";
    $db_pass = "password";
    $db_name = "database";
    $db_table = "users";
    
    $connect = mysql_connect($db_addr,$db_user,$db_pass);
    
    if (!($connect)) {
    	echo "There was a problem.";
    	exit();
    }
    
    if (!(mysql_select_db($db_name))) {
    	echo "There was a problem.";
    	exit();
    }
    

    Create a table

    $make_table = "create table $db_table (first_name varchar(50) not null, last_name varchar(50) not null)";
    
    if (mysql_query($make_table)) {
    	$message = "Table Created";
    }
    else {
    	$message = "There was a problem.";
    }
    

    Add a record

    if (mysql_query("insert into $db_table values ('$FirstName', '$LastName')")) {
    	$message = "User Created";
    }
    else {
    	$message = "There was a problem.";
    }
    

    Change a record

    if (mysql_query("update $db_table set first_name = '$NewFirstName' where last_name = '$LastName'")) {
    	$message = "User Edited";
    }
    else {
    	$message = "There was a problem.";
    }
    

    Delete a record

    if (mysql_query("delete from $db_table where last_name = '$LastName'")) {
    	$message = "User Deleted";
    }
    else {
    	$message = "There was a problem.";
    }
    

    Display a record

    $user_query = mysql_query("select * from $db_table");
    
    while ($user_data = mysql_fetch_array($user_query)) {
    	echo $user_data["first_name"] . " " . $user_data["last_name"] . "<br>";
    }
    

      thanks! im playing with it now..

      I have one problem on a web page. i dont know if its the way i installed php or not, im trying to put a form on there, and the submit button looks like this...

      form action="<?=$PHP_SELF?>" method="post">

      which i thought is right from the examples i see, but it gives me this....

      <b>Notice</b>: Undefined variable: PHP_SELF in <b>C:\webs\allurables.com\search.php</b> on line <b>5</b><br />
      " method="post">

      looks like it doesnt understand php_self... is there something im missing?

        Hi. Try using $_SERVER['PHP_SELF']

          man your good....

          ok, one LAST (Ok i dont promise) question:

          i cant access a form field....

          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
          <font size=3 face=arial color=#330066><b>Search by: &nbsp </b></font>
          <SELECT NAME="value">
          <option value="id">ID
          <option value="name">Name
          <option value="icq">ICQ
          <option value="email">Email
          </select>
          <br><br>
          <input type=text name="keyword" size=35>
          <input type=submit value="Search">

          .
          .

          $sql="SELECT * FROM table01 WHERE id=$keyword";

          gives me:

          Undefined variable keyword...

            Hi. Not tested...

            <?php
            // make and add data to the following table or edit the below code to correctly match a current table:
            // create table table01 (keyword varchar(50) not null, keyid varchar(25) not null, keyinfo text not null)
            
            // configure database variables
            $db_addr = "localhost";
            $db_user = "username";
            $db_pass = "password";
            $db_name = "database";
            
            $keyword = trim($_GET['keyword']);
            $keyid = trim($_GET['keyid']);
            
            $searchbox = <<<END
            <html><body><form action="$_SERVER['PHP_SELF']" method="post">
            <font size="3" face="arial,sans-serif" color="#330066"><b>Search by: </b></font>
            	<select name="keyid">
            		<option value="id">ID</option>
            		<option value="name">Name</option>
            		<option value="icq" selected>ICQ</option>
            		<option value="email">Email</option>
            	</select>
            <br><br>
            <input type="text" name="keyword" size="35">
            <input type="submit" value="Search">
            </form></body></html>
            END;
            
            if ((isset($keyword)) && (isset($keyid)) && (strlen($keyword) > 0) && (strlen($keyid) > 0)) {
            	if (!(get_magic_quotes_gpc())) {
            		$keyword = addslashes($keyword);
            		$keyid = addslashes($keyid);
            	}
            	$connect = mysql_connect($db_addr,$db_user,$db_pass);
            	if (!($connect)) {
            		echo "There was a problem.";
            		exit(); 
            	}
            	if (!(mysql_select_db($db_name))) {
            		echo "There was a problem.";
            		exit();
            	}
            	$sql_result = mysql_query("select * from table01 where keyword like '%$keyword%' and keyid = '$keyid'");
            	$cnt = 1;
            	while($data = mysql_fetch_array($sql_result)) {
            		echo $cnt . ": " . $data["keyinfo"] . "<br><br>";
            		$cnt++;
            	}
            	echo $searchbox;
            }
            else {
            	echo $searchbox;
            }
            ?>
            

            Your turn. 😉

              Thanks to everyone.

              I managed to piece together a php web application to be used as a model, it searches, adds, updates and deletes to a mysql database. Very simple, no complicated web pages opr anything, using it to build off of for other apps.

              I'm attaching it, because im sure ANY newbie like me will look for something like this.

              now keep in mind that i never did php before last night.. so maybe someone can look at it and critique.

              --Karl

                Write a Reply...