1

I am trying to develop a web server to be used in dice rolling and message sending for our dungeons and dragons group. I can create and drop tables fine, and I believe my view function is working. However it is saying 0 rows are being returned on its result. So I believe there is an issue with my insert data sql query.

Insert data php page:

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'anthony';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
mysql_select_db( 'messages' );




    $number = 0;
   echo "Name: {$_POST['name']}<br />";
   echo "Subject: {$_POST['subject']}<br />";
   echo "Message: {$_POST['message']}<br /><br />";
   if(strcmp($_POST['die'],"D100") == 0 ){
       $number = (mt_rand ( 1 , 100 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }
   if(strcmp($_POST['die'],"D20") == 0 ){
       $number = (mt_rand ( 1 , 20 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }
   if(strcmp($_POST['die'],"D12") == 0 ){
       $number = (mt_rand ( 1 , 12 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }
   if(strcmp($_POST['die'],"D10") == 0 ){
       $number = (mt_rand ( 1 , 10 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }
   if(strcmp($_POST['die'],"D8") == 0 ){
       $number = (mt_rand ( 1 , 8 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }
   if(strcmp($_POST['die'],"D6") == 0 ){
       $number = (mt_rand ( 1 , 6 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }
   if(strcmp($_POST['die'],"D3") == 0 ){
       $number = (mt_rand ( 1 , 3 )* + $_POST['amount'] ) + $_POST['modifier'];
       echo $number;
   }



$sql = "INSERT INTO message_tbl (message_name, message_subject, message_txt, message_amount, message_die, message_modifier, message_roll)
VALUES ('".$_POST['name']."', '".$_POST['subject']."', '".$_POST['message']."', '".$_POST['amount']."', '".$_POST['die']."', '".$_POST['modifier']."', '".$number."')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close();


?>

This shows my roll and all the info from the previous form, however it does not do any of these echos:

if ($conn->query($sql) == TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

My view data PHP page:

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'anthony';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
mysql_select_db( 'messages' );


$query = "SELECT * FROM message_tbl";
$result = mysql_query($query);
printf("Select returned %d rows.\n", $result->num_rows); //prints how many rows returned

echo "<table>";
while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
echo "id: " . $row["message_id"]. " " . $row["message_name"]. " " . $row["message_subject"]. " " . $row["message_txt"]. " " . $row["message_amount"]. " " . $row["message_die"]. " " . $row["message_modifier"]. " " . $row["message_roll"]. "<br>";
}

echo "</table>"; //Close the table in HTML

mysql_close(); //Make sure to close out the database connection
?>

My view data page returns with "select returned 0 rows" and nothing else.

zoid230
  • 47
  • 5
  • 1
    Because `mysql_connect` doesn't return object. Refer to a manual. – u_mulder Jan 24 '16 at 14:01
  • I got most of my documentation from W3Schools: http://www.w3schools.com/php/php_mysql_insert.asp – zoid230 Jan 24 '16 at 14:03
  • 1
    Do you see `new mysqli` with `i` in the end. Do you have this `i` in your codes? – u_mulder Jan 24 '16 at 14:05
  • No I dont use mysqli in my code anywhere. – zoid230 Jan 24 '16 at 14:07
  • So if you think that `mysql` works the same as `mysqli` (with `i`) - then I have so bad news for ya. – u_mulder Jan 24 '16 at 14:09
  • http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php and a related question. – u_mulder Jan 24 '16 at 14:09
  • But I dont want to use mysqli? Every resource I have used on the net has been using mysql. I am using mysql in all my other php page. Are you saying the W3 schools resource I linked you too is incorrect in some way? – zoid230 Jan 24 '16 at 14:11
  • W3Schools resource uses `mysqli`. You use `mysql`. These are __completely different apis__. And have __different methods__. – u_mulder Jan 24 '16 at 14:14
  • 2
    If you don't want to use `mysqli` - fine. Use `mysql`, but be aware that `mysql` extension is deprecated of php5.5 and removed in php7. So soon your `mysql` codes will not work. And if you use `mysql` - then don't refer to manuals for `mysqli`. – u_mulder Jan 24 '16 at 14:15
  • That `INSERT` query has a rather large SQL injection vulnerability - don't go live with this! – halfer Jan 24 '16 at 19:22
  • `Every resource I have used on the net has been using mysql` - those tutorials are out of date, by several years. `Are you saying the W3 schools resource I linked you to is incorrect?` - the link you offered demonstrates MySQLi and PDO/MySQL, both of which are fine. – halfer Jan 24 '16 at 19:24

1 Answers1

0

Sorry, I'm no expert in php, I have only just started developing for the web and am learning as I go. But as far as I can tell mysql is being deprecated, and the use use of MySQLi is recommended. It may not be the case but I noticed that you are using the OO and procedural style in you code, I have only ever stuck with one method in whatever code I have written. Probably not the answer you are after, but I am not able to leave a comment.

Also where php is concerned, I would recommend using php.net as a better resource than w3schools.

Mr.Z
  • 77
  • 5