Page 1 of 1

Weird problem

Posted: Sat May 10, 2003 9:10 am
by ampersand
I use this code to insert new users, but when I click submit they are not added to the database. Can anyone please tell me whats wrong with this code ? I use almost the similar code to inserts cd's and that works.. :?

Code: Select all

<?php require_once('Connections/cds.php'); ?>

<?php 
 $db_connection = mysql_connect($hostname_cds , $username_cds , $password_cds) or die ("Could not connect to database");
 mysql_select_db ($database_cds , $db_connection) or die ("Could not find database")
 ?>
<form action="<?php echo $PHP_SELF?>" method="post" name="form1" target="_self">
        <table border="0" cellpadding="2" cellspacing="0">
          <tr> 
            <td>Insert user:</td>
            <td><input class="x" name="insert_user" type="text" id="user_name" size="32"></td>
          </tr>
		  <tr>
		  	<td>Delete user:</td>
		  	<td><select class="x" name="delete_user">
			<option>Choose</option>
          <?php

			$category_query = "SELECT * FROM user ORDER BY user_id DESC";

			$query_result = mysql_query ($category_query, $db_connection);

			while ($row = mysql_fetch_object ($query_result))
			{
               print "<option value="" . $row->user_id . "">" . $row->user_name . "</option>";
			}
		
?>
        </select></td>
		  </tr>
          <tr> 
            <td align="center">&nbsp; </td>
            <td><input name="Submit" type="submit" value="Submit"></td>
          </tr>
        </table>
      </form>

<?php


		if ($insert_user <> " ")
		{ 
			$result = mysql_query("INSERT INTO user(user_name) VALUES '$insert_user'", $db_connection);
		}
		
		if ($insert_user == "")
		{ 
			$result = mysql_query("DELETE FROM user WHERE user_id LIKE '$delete_user'", $db_connection);
		}
		
?>
Thanks in advance
ampersand

Posted: Sat May 10, 2003 9:29 am
by AVATAr
Check this first: viewtopic.php?t=511

Posted: Sat May 10, 2003 11:16 pm
by evilcoder
You may want to add some error finding queries while your in development stages:

Code: Select all

<?php
require_once('Connections/cds.php'); ?> 

<?php 
$db_connection = mysql_connect($hostname_cds , $username_cds , $password_cds);

if ( !$db_connection )
{
   echo "Couldnt connect to server " . mysql_error();
   exit;
}
 
$db_select = mysql_select_db ($database_cds , $db_connection);

if ( !$db_select )
{
   echo "Couldnt connect to database" . mysql_error();
   exit;
}

?> 
<form action="<?php echo $PHP_SELF?>" method="post" name="form1" target="_self"> 
        <table border="0" cellpadding="2" cellspacing="0"> 
          <tr> 
            <td>Insert user:</td> 
            <td><input class="x" name="insert_user" type="text" id="user_name" size="32"></td> 
          </tr> 
        <tr> 
           <td>Delete user:</td> 
           <td><select class="x" name="delete_user"> 
         <option>Choose</option> 
          <?php 

         $category_query = "SELECT * FROM user ORDER BY user_id DESC"; 

         $query_result = mysql_query ($category_query, $db_connection); 

         while ($row = mysql_fetch_object ($query_result)) 
         { 
               print "<option value="" . $row->user_id . "">" . $row->user_name . "</option>"; 
         } 
       
?> 
        </select></td> 
        </tr> 
          <tr> 
            <td align="center">  </td> 
            <td><input name="Submit" type="submit" value="Submit"></td> 
          </tr> 
        </table> 
      </form> 

<?php 


      if ($insert_user <> " ") 
      { 
         $result = mysql_query("INSERT INTO user(user_name) VALUES '$insert_user'", $db_connection); 
         
         if ( !$result )
         {
            echo "Couldnt insert information " . mysql_error();
            exit;
         }
      } 
       
      if ($insert_user == "") 
      { 
         $result = mysql_query("DELETE FROM user WHERE user_id LIKE '$delete_user'", $db_connection); 
         
            if ( !$result )
         {
            echo "Couldnt delete information " . mysql_error();
            exit;
         }
      } 
       
?>
Hope this helps.

Posted: Sun May 11, 2003 6:08 am
by ampersand
Thanks.