0

i'm making a customer script for some friends. they have a gsm/pc service. i've made this so far:

            <div id="somedialog" class="dialog">
                <div class="dialog__overlay"></div>
                <div class="dialog__content">
                    <form method="post" action="mysql.php"><p align="left"><font color="white" size="5" face="Tahoma">NUME: <textarea name="nume" style="overflow:hidden" rows="1" cols="17"></textarea> PRENUME:  <textarea name="prenume" style="overflow:hidden" rows="1" cols="15"></textarea><br> TIP DEVICE: <textarea name="tipd" style="overflow:hidden" rows="0" cols="15"></textarea>BRAND: <textarea name="brand" style="overflow:hidden" rows="1" cols="15"></textarea> MODEL: <textarea name="model" style="overflow:hidden" rows="1" cols="15"></textarea><br>PROBLEMA: <textarea name="problema" style="overflow:hidden" rows="1" cols="15"></textarea> DETALII: <textarea name="detalii" style="overflow:hidden" rows="1" cols="15"></textarea></p><div><button class="action" data-dialog-close>Inchide</button><button class="action" data-dialog-close>Creaza</button></div></font><input type="submit" value="Submit"></form>
                </div>
            </div>
        </div><!-- /content -->

my mysql.php page:

<?php
$dbhandle = mysql_connect("localhost", "root", "", "client");

if (!$dbhandle) {
    die("Connection failed: " . mysql_connect_error());
}
echo "Connected successfully";

$nume=$_POST['nume'];
$prenume=$_POST['prenume'];
$tipd=$_POST['tipd'];
$brand=$_POST['brand'];
$model=$_POST['model'];
$problema=$_POST['problema'];
$detalii=$_POST['detalii'];



mysql_query("INSERT INTO client (Nume, Prenume) VALUES ('$nume', '$prenume')");

?>

When i run the script it gives me No database selected.

This is a screenshot of my database: screenshot

could you help me please?

Radu Cosmin
  • 1
  • 1
  • 1
  • 1
    Warning: Your code is *wide open* to **SQL injection**. Basically, you are executing any code your users feel like sending you. Code which could do anything to your database. – David Jun 10 '16 at 16:31
  • 1
    mysql_* functions have been deprecated, and since removed from PHP7, so I'd suggest looking at PDO / MySQLi and prepared statements. Wrt your actual question, have a look at [mysql_select_db](http://php.net/manual/en/function.mysql-select-db.php). – Jonnix Jun 10 '16 at 16:31
  • You are using the parameters that fit with a `mysqli_connect()` but you are using the `mysql_connect()` API calls. **Which** do you think you **should** be using? – RiggsFolly Jun 10 '16 at 16:48

2 Answers2

0

Use mysql_select_db:

$dbhandle = mysql_connect("localhost", "root", "");

if (!$dbhandle) {
    die("Connection failed: " . mysql_connect_error());
}
mysql_select_db('my_database_name', $dbhandle);
....

The best option here change to mysqli_* functions, and create a specific user for your table, with some grants:

$dbhandle = mysqli_connect("localhost", "client_db_user", "client_db_password", "client");
...
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
  • No dont! `mysql_select_db()` is more suited to switching between 2 or more databases using the same connection. Anyway I would bet the database is not called `client` – RiggsFolly Jun 10 '16 at 16:36
  • The correct way is to have a specific user for the database. The OP is probably using root user. – Felippe Duarte Jun 10 '16 at 16:37
  • Beg your pardon, I see OP is using `mysql_` API I thought it was `mysqlI_` API. So all you need to do is **remove the database name from the `mysql_connect_db()` statement.** – RiggsFolly Jun 10 '16 at 16:44
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) its really pretty easy – RiggsFolly Jun 10 '16 at 16:45
  • No problem. Of course, edited, because it's better and cleaner. – Felippe Duarte Jun 10 '16 at 16:45
  • **No** The `mysql_connect()` API call does not have a parameter for a database name, but you like the OP are using one. I think we are all getting **very** confused here with this question – RiggsFolly Jun 10 '16 at 16:49
0

After you get connected to the database, change your mysql_query into variable to make it easier to identify.

$query = mysql_query("your query");

if ($query) {
echo "Insertion success";
} else {
echo "Failed";
echo mysql_error();
};

And also add var_dump($_POST); to identify which variable did you send from the previous file.

M Ansyori
  • 429
  • 6
  • 21