简体   繁体   中英

No database selected php to mysql

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?

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");
...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM