Weird problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Weird problem

Post 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
Last edited by ampersand on Sat May 10, 2003 9:34 am, edited 1 time in total.
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

Check this first: viewtopic.php?t=511
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post 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.
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

Thanks.
Post Reply